2012-03-25 97 views
4

編輯:我證實這是Python中的錯誤。這是bug http://bugs.python.org/issue10332(我提出了一個新的錯誤,維護者指出我給了10332)。我將Python源代碼倉庫中的多處理目錄複製到我的項目目錄中,並且現在可以正常運行測試用例。蟒蛇多處理與maxtasksperchild

這個看似簡單的程序不適用於我,除非我刪除maxtasksperchild參數。我究竟做錯了什麼?

from multiprocessing import Pool 
import os 
import sys 

def f(x): 
    print "pid: ", os.getpid(), " got: ", x 
    sys.stdout.flush() 
    return [x, x+1] 

def cb(r): 
    print "got result: ", r 

if __name__ == '__main__': 
    pool = Pool(processes=1, maxtasksperchild=9) 
    keys = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
    result = pool.map_async(f, keys, chunksize=1, callback=cb) 
    pool.close() 
    pool.join() 

當我運行它,我得到:

$ python doit.py 
pid: 6409 got: 1 
pid: 6409 got: 2 
pid: 6409 got: 3 
pid: 6409 got: 4 
pid: 6409 got: 5 
pid: 6409 got: 6 
pid: 6409 got: 7 
pid: 6409 got: 8 
pid: 6409 got: 9 

而且它掛起。也就是說,處理第10個元素的新工人並沒有被產生。

在另一端,我看到:

$ ps -C python 
    PID TTY   TIME CMD 
6408 pts/11 00:00:00 python 
6409 pts/11 00:00:00 python <defunct> 

這是在Ubuntu 11.10進行運行蟒蛇2.7.2+(從Ubuntu的軟件包安裝)。

+0

我認爲這是一個Python中的錯誤。我對pool.close()的調用(文檔說我應該在調用pool.join()之前調用它),將標誌pool._state設置爲CLOSE。函數Pool._handle_workers依賴於該標誌爲'RUN'來啓動新的工作進程。 該錯誤的一個解決方法是在map_async調用約10秒後休眠,直到調用pool.close()。我可能會提交一個針對python的bug。 – user188012 2012-03-25 12:03:31

+0

我可以證實這個hevaiour。擁有python 2.7.2,面臨與maxtasksperchild = 1相同的問題。在所有任務成功完成後,腳本停在最後的pool.join(),留下所有的子進程殭屍()。從池創建中刪除此參數 - 解決了問題。 – 2013-09-13 16:33:30

+0

是否解決了? – user3467349 2015-01-23 02:11:35

回答

-2

我從來沒有使用過的python多線程,但我想你想maxtasksperchild = 10在這一行:pool = Pool(processes=1, maxtasksperchild=9)和輸出後的變化是:

pid: 8436 got: 1 
pid: 8436 got: 2 
pid: 8436 got: 3 
pid: 8436 got: 4 
pid: 8436 got: 5 
pid: 8436 got: 6 
pid: 8436 got: 7 
pid: 8436 got: 8 
pid: 8436 got: 9 
pid: 8436 got: 10 
got result: [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10], [10, 11]] 
+0

很高興聽到您獲得的輸出與我的輸出相同。但是,您所寫的內容並不能回答我的問題。 maxtasksperchild = 10可以工作,因爲不需要重新配置工作進程。爲什麼不maxtasksperchild = 9工作? – user188012 2012-03-25 10:42:44

0

maxtasksperchild意味着一個處理執行任務的最大數量。

0

此問題已在python3中修復。

pid: 18316 got: 1 
pid: 18316 got: 2 
pid: 18316 got: 3 
pid: 18316 got: 4 
pid: 18316 got: 5 
pid: 18316 got: 6 
pid: 18316 got: 7 
pid: 18316 got: 8 
pid: 18316 got: 9 
pid: 18317 got: 10 
got result: [[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9], [9, 10], [10, 11]]