Python - appending to same file from multiple threads

后端 未结 3 1327
一个人的身影
一个人的身影 2020-12-02 14:19

I\'m writing an app that appends lines to the same file from multiple threads.

I have a problem in which some lines are appended without a new line.

Any solu

3条回答
  •  半阙折子戏
    2020-12-02 15:02

    The solution is to write to the file in one thread only.

    import Queue  # or queue in Python 3
    import threading
    
    class 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 here
    
    pathqueue = Queue.Queue()
    resultqueue = Queue.Queue()
    paths = getThisFromSomeWhere()
    
    output = codecs.open('file', 'a')
    
    # spawn threads to process
    for i in range(0, 5):
        t = ProcessThread(pathqueue, resultqueue)
        t.setDaemon(True)
        t.start()
    
    # spawn threads to print
    t = PrintThread(resultqueue)
    t.setDaemon(True)
    t.start()
    
    # add paths to queue
    for path in paths:
        pathqueue.put(path)
    
    # wait for queue to get empty
    pathqueue.join()
    resultqueue.join()
    

提交回复
热议问题