簡體   English   中英

Python多處理導致許多僵屍進程

[英]Python Multiprocessing leading to many zombie processes

我一直在使用工作池來實現python的多處理庫。 我實現了以下代碼

import main1
t1 = time.time()
p = Pool(cores) 
result = p.map(main1, client_list[client])
if result == []:
    return []
p.close()
p.join()
print "Time taken in performing request:: ", time.time()-t1
return shorted(result)

但是,在運行該過程一段時間后,我的應用程序運行后台進程很多。 這是為我的應用程序做ps aux之后的快照

顯示所有僵屍進程的快照

現在,我已經在stackoverflow上閱讀了很多類似的問題,比如如何殺死多處理模塊創建的僵屍進程? 它要求使用我已經實現的.join(),並且我從這里學習了如何從Python Multiprocessing Kill Processes中殺死所有這些進程 但我想知道我的代碼可能出錯的地方。 我將無法在main1函數中共享所有代碼,但我已將整個代碼塊放在try catch塊中,以避免主代碼中的錯誤導致僵屍進程的情況。

def main1((param1, param2, param3)):
    try:
       resout.append(some_data) //resout in case of no error
    except:
        print traceback.format_exc()
        resout = []  //sending empty resout in case of error
    return resout

我對並行編程和調試問題的概念還很陌生,結果很棘手。非常感謝任何幫助。

通常最常見的問題是池已創建但未關閉。

我知道保證池關閉的最好方法是使用try / finally子句:

try:
    pool = Pool(ncores)
    pool.map(yourfunction, arguments)
finally:
    pool.close()
    pool.join()

如果你不想為multiprocessing parmap ,我寫了一個名為parmap的簡單包,它包含了多處理,使我的生活(以及可能你的生活)更容易。

pip install parmap

import parmap
parmap.map(yourfunction, arguments)

從parmap用法部分:

  • 簡單的並行示例:

     import parmap y1 = [myfunction(x, argument1, argument2) for x in mylist] y2 = parmap.map(myfunction, mylist, argument1, argument2) y1 == y2 
  • 迭代元組列表:

     # You want to do: z = [myfunction(x, y, argument1, argument2) for (x,y) in mylist] z = parmap.starmap(myfunction, mylist, argument1, argument2) # You want to do: listx = [1, 2, 3, 4, 5, 6] listy = [2, 3, 4, 5, 6, 7] param = 3.14 param2 = 42 listz = [] for (x, y) in zip(listx, listy): listz.append(myfunction(x, y, param1, param2)) # In parallel: listz = parmap.starmap(myfunction, zip(listx, listy), param1, param2) 

暫無
暫無

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

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