Child to parent communication in Python multiprocessing

前端 未结 2 1897
误落风尘
误落风尘 2021-01-12 18:19

I am writing a python script that will parse through a file quickly by sending lines to different processes to handle. At the end, I want the parent to receive the results f

2条回答
  •  星月不相逢
    2021-01-12 19:04

    You can pass values back through an results queue.

    In LineConsumer:

    def __init__(self, queue, result_queue):
       self.result_queue = result_queue
       # ...
    
    def terminate(self):
       self.results_queue.put(self.lines)
       super(LineConsumer, self).terminate()
    

    In Parser:

    queue = JoinableQueue(100)
    result_queue = Queue()
    # ...
      lc = LineConsumer(queue, result_queue)
    # ...
    for p in consumers:
      p.terminate()
      p.join()
    
    while True:
      try:
        print results.queue.get(False)
      except Queue.Empty: # need to import Queue
        break
    

提交回复
热议问题