解决方案是仅在一个线程中写入文件。
import Queue # or queue in Python 3import threadingclass PrintThread(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: result = self.queue.get() self.printfiles(result) self.queue.task_done()class ProcessThread(threading.Thread): def __init__(self, in_queue, out_queue): threading.Thread.__init__(self) self.in_queue = in_queue self.out_queue = out_queue def run(self): while True: path = self.in_queue.get() result = self.process(path) self.out_queue.put(result) self.in_queue.task_done() def process(self, path): # Do the processing job herepathqueue = Queue.Queue()resultqueue = Queue.Queue()paths = getThisFromSomeWhere()output = precs.open('file', 'a')# spawn threads to processfor i in range(0, 5): t = ProcessThread(pathqueue, resultqueue) t.setDaemon(True) t.start()# spawn threads to printt = PrintThread(resultqueue)t.setDaemon(True)t.start()# add paths to queuefor path in paths: pathqueue.put(path)# wait for queue to get emptypathqueue.join()resultqueue.join()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)