我正在寫一個應用程序,將行附加到來自多個線程的同一個文件。蟒蛇 - 附加到同一個文件從多個線程
我有一個問題,其中一些行被添加而沒有新行。
任何解決方案?
class PathThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def printfiles(self, p):
for path, dirs, files in os.walk(p):
for f in files:
print(f, file=output)
def run(self):
while True:
path = self.queue.get()
self.printfiles(path)
self.queue.task_done()
pathqueue = Queue.Queue()
paths = getThisFromSomeWhere()
output = codecs.open('file', 'a')
# spawn threads
for i in range(0, 5):
t = PathThread(pathqueue)
t.setDaemon(True)
t.start()
# add paths to queue
for path in paths:
pathqueue.put(path)
# wait for queue to get empty
pathqueue.join()
發佈一些代碼,這將有助於。 – 2012-08-16 09:10:00
追加一個新行。 – Kuf 2012-08-16 09:11:19
聽起來像* impossibru *。 – plaes 2012-08-16 09:11:27