How to use multiprocessing pool.map with multiple arguments?

前端 未结 20 3496
-上瘾入骨i
-上瘾入骨i 2020-11-21 11:24

In the Python multiprocessing library, is there a variant of pool.map which supports multiple arguments?

text = "test"
def         


        
20条回答
  •  星月不相逢
    2020-11-21 11:40

    In the official documentation states that it supports only one iterable argument. I like to use apply_async in such cases. In your case I would do:

    from multiprocessing import Process, Pool, Manager
    
    text = "test"
    def harvester(text, case, q = None):
     X = case[0]
     res = text+ str(X)
     if q:
      q.put(res)
     return res
    
    
    def block_until(q, results_queue, until_counter=0):
     i = 0
     while i < until_counter:
      results_queue.put(q.get())
      i+=1
    
    if __name__ == '__main__':
     pool = multiprocessing.Pool(processes=6)
     case = RAW_DATASET
     m = Manager()
     q = m.Queue()
     results_queue = m.Queue() # when it completes results will reside in this queue
     blocking_process = Process(block_until, (q, results_queue, len(case)))
     blocking_process.start()
     for c in case:
      try:
       res = pool.apply_async(harvester, (text, case, q = None))
       res.get(timeout=0.1)
      except:
       pass
     blocking_process.join()
    

提交回复
热议问题