2013-08-28 108 views
6

我正在學習如何使用Python多處理庫。然而,當我通過一些例子時,我結束了許多在我的後臺運行的python進程。Python多處理殺死進程

其中example的樣子如下:

from multiprocessing import Process, Lock 

def f(l, i): 
    l.acquire() 
    print 'hello world', i 
    l.release() 

if __name__ == '__main__': 
    lock = Lock() 

    for num in range(10): # I changed the number of iterations from 10 to 1000... 
     Process(target=f, args=(lock, num)).start() 

現在,這裏是我的 'TOP' 命令的屏幕截圖:

88950 Python  0.0 00:00.00 1 0 9  91 1584K 5856K 2320K 1720K 2383M 82441 1  sleeping 1755113321 799 
88949 Python  0.0 00:00.00 1 0 9  91 1584K 5856K 2320K 1720K 2383M 82441 1  sleeping 1755113321 798 
88948 Python  0.0 00:00.00 1 0 9  91 1580K 5856K 2316K 1716K 2383M 82441 1  sleeping 1755113321 797 
88947 Python  0.0 00:00.00 1 0 9  91 1580K 5856K 2316K 1716K 2383M 82441 1  sleeping 1755113321 796 
88946 Python  0.0 00:00.00 1 0 9  91 1576K 5856K 2312K 1712K 2383M 82441 1  sleeping 1755113321 795 
88945 Python  0.0 00:00.00 1 0 9  91 1576K 5856K 2312K 1712K 2383M 82441 1  sleeping 1755113321 794 
88944 Python  0.0 00:00.00 1 0 9  91 1576K 5856K 2312K 1712K 2383M 82441 1  sleeping 1755113321 794 
88943 Python  0.0 00:00.00 1 0 9  91 1572K 5856K 2308K 1708K 2383M 82441 1  sleeping 1755113321 792 
88942 Python  0.0 00:00.00 1 0 9  91 1568K 5856K 2304K 1708K 2383M 82441 1  sleeping 1755113321 790 
88941 Python  0.0 00:00.00 1 0 9  91 1564K 5856K 2300K 1704K 2383M 82441 1  sleeping 1755113321 789 
88938 Python  0.0 00:00.00 1 0 9  91 1564K 5856K 2300K 1704K 2383M 82441 1  sleeping 1755113321 788 
88936 Python  0.0 00:00.00 1 0 9  91 1576K 5856K 2296K 1716K 2383M 82441 1  sleeping 1755113321 787 
88935 Python  0.0 00:00.00 1 0 9  91 1560K 5856K 2296K 1700K 2383M 82441 1  sleeping 1755113321 787 
88934 Python  0.0 00:00.00 1 0 9  91 1560K 5856K 2296K 1700K 2383M 82441 1  sleeping 1755113321 786 
88933 Python  0.0 00:00.00 1 0 9  91 1556K 5856K 2292K 1696K 2383M 82441 1  sleeping 1755113321 785 
88932 Python  0.0 00:00.00 1 0 9  91 1556K 5856K 2292K 1696K 2383M 82441 1  sleeping 1755113321 784 
88931 Python  0.0 00:00.00 1 0 9  91 1552K 5856K 2288K 1692K 2383M 82441 1  sleeping 1755113321 783 
88930 Python  0.0 00:00.00 1 0 9  91 1612K 5856K 2288K 1752K 2383M 82441 1  sleeping 1755113321 783 
88929 Python  0.0 00:00.00 1 0 9  91 1588K 5856K 2288K 1728K 2383M 82441 1  sleeping 1755113321 782 
88927 Python  0.0 00:00.00 1 0 9  91 1608K 5856K 2284K 1748K 2383M 82441 1  sleeping 1755113321 781 
88926 Python  0.0 00:00.00 1 0 9  91 1548K 5856K 2284K 1688K 2383M 82441 1  sleeping 1755113321 780 
88924 Python  0.0 00:00.00 1 0 9  91 1556K 5856K 2276K 1700K 2383M 82441 1  sleeping 1755113321 778 
88923 Python  0.0 00:00.00 1 0 9  91 1540K 5856K 2276K 1684K 2383M 82441 1  sleeping 1755113321 777 
88922 Python  0.0 00:00.00 1 0 9  91 1540K 5856K 2276K 1684K 2383M 82441 1  sleeping 1755113321 776 
88921 Python  0.0 00:00.00 1 0 9  91 1536K 5856K 2272K 1680K 2383M 82441 1  sleeping 1755113321 774 
88920 Python  0.0 00:00.00 1 0 9  91 1528K 5856K 2264K 1672K 2383M 82441 1  sleeping 1755113321 771 
88919 Python  0.0 00:00.00 1 0 9  91 1528K 5856K 2264K 1672K 2383M 82441 1  sleeping 1755113321 771 
88918 Python  0.0 00:00.00 1 0 9  91 1528K 5856K 2264K 1672K 2383M 82441 1  sleeping 1755113321 770 
.... 
  1. 我不知道怎麼殺他們一氣呵成。

    ps ... | grep python .... kill?

  2. 我需要添加什麼類型的python代碼才能避免這種悲慘的情況。謝謝!

回答

3

您需要。加入()在工人隊列的過程,這將他們鎖定到調用應用程序,直到所有的人都成功,或者當父母被殺害殺死,並在後臺模式下運行。

http://forums.xkcd.com/viewtopic.php?f=11&t=94726

end daemon processes with multiprocessing module

http://docs.python.org/2/library/multiprocessing.html#the-process-class

http://www.python.org/dev/peps/pep-3143/#correct-daemon-behaviour

+1

這是'你應該有...'的代碼,以避免這種情況再次發生,對吧?你知道如何殺死多處理產生的所有進程嗎? –

5

布雷克VandeMerwe指出答案是上市及以下希望解釋可以爲其他用戶有所幫助:

Original Author

kill -9 `ps -ef | grep test.py | grep -v grep | awk '{print $2}'` 

闡釋:

  1. 「PS -ef」:顯示所有的過程,包括那些沒有控制終端,它是完全由多庫產生的無數方法。

  2. 「grep test.py」:找到這個腳本生成的所有進程,這是我的python腳本的名字。

  3. 「的grep -v grep的」:排除在 '殺人名單' 上的grep操作本身

  4. 「的awk '{打印$ 2}'」:用AWK到每一個記錄分成行並打印出在這種情況下,第二行是進程id colum。

  5. 「kill -9」是強制殺死進程,參數應該是UID。前面步驟的完整輸出由「`」放在一起,「`」是常規鍵盤上編號1左側的字符。將其視爲一個變量並通過殺價值。

+1

-1。沒有! [不](HTTP://unix.stackexchange。com/q/8916/20714)[just](http://partmaps.org/era/unix/award.html#uuk9letter)['SIGKILL'](https://en.wikipedia.org/wiki/Unix_signal# SIGKILL)[first!](http://superuser.com/a/137400/144700) –