Python-从多个线程追加到同一文件

Python-从多个线程追加到同一文件,第1张

Python-从多个线程追加到同一文件

解决方案是仅在一个线程中写入文件。

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()


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5644755.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存