簡體   English   中英

Python多處理程序未運行到最后

[英]Python multiprocessing program not running to the end

我是python中的多處理新手。

基本上,我的問題場景是我想在一組表(例如2個表)上並行運行python腳本。

在這里,我的python腳本並行地從每個表中讀取數據,然后將這些表中的數據寫入另一個表中。

我已經編寫了以下代碼片段來創建多進程python腳本。 但是,當我運行腳本時,它沒有完成,也沒有拋出任何錯誤消息。

count = multiprocessing.cpu_count()
pool = multiprocessing.Pool(processes=count)
args = [ ('yelp','localhost:9160','cassa1','flight88'), ('yelp','localhost:9160','cassa1','flight96') ]
for a in args:
    print a
    pool.apply_async(user_input,a)

對此我深感困惑,並深陷其中,對此不勝感激。

您的腳本在子進程完成其任務之前退出。 在末尾添加:

pool.close() # no more tasks
pool.join()  # wait for the remaining tasks to complete

另外,您可以改用pool.imap*()方法:

from multiprocessing import Pool

def safe_user_input(args):
    try:
         return user_input(*args), None
    except Exception as e:
         return None, str(e)

if __name__=="__main__":       
   tables = [
        ('yelp','localhost:9160','cassa1','flight88'),
        ('yelp','localhost:9160','cassa1','flight96')
   ]

   pool = Pool() # use all available CPUs
   for result, error in pool.imap_unordered(safe_user_input, tables):
       if error is None: # no error
          print(result)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM