我正在写一个应用程序,将行附加到来自多个线程的同一个文件。蟒蛇 - 附加到同一个文件从多个线程
我有一个问题,其中一些行被添加而没有新行。
任何解决方案?
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