Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python多处理Kill进程_Python_Macos_Multiprocessing - Fatal编程技术网

Python多处理Kill进程

Python多处理Kill进程,python,macos,multiprocessing,Python,Macos,Multiprocessing,我正在学习如何使用Python多处理库。然而,在我阅读一些示例的同时,我在后台运行了许多python进程 其中一个选项如下所示: 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

我正在学习如何使用Python多处理库。然而,在我阅读一些示例的同时,我在后台运行了许多python进程

其中一个选项如下所示:

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
....
  • 我不知道如何一下子杀死他们

    ps…|grep python。。。。杀人

  • 我需要添加什么样的python代码来避免再次出现这种糟糕的情况。谢谢

  • 您需要对工作队列中的进程执行
    .join()
    ,这将把它们锁定到调用应用程序,直到所有进程都成功或在父进程被终止时终止,并在守护进程模式下运行它们


    下面列出并解释了Blake VandeMerwe指出的答案,希望能对其他用户有所帮助:

    :

    解释:

  • “ps-ef”:显示所有进程,包括没有控制终端的进程,这些进程正是由多处理库生成的无数进程

  • “grep test.py”:查找此脚本生成的所有进程,这是我的python脚本的名称

  • “grep-v grep”:将grep操作本身从“终止列表”中排除

  • “awk'{print$2}'”:使用awk将每个记录分隔成行,并打印出第二行,在本例中,第二行是进程id列

  • “kill-9”是强制kill进程,参数应该是UID。前面步骤的完整输出由“`”组合在一起,这是常规键盘上数字1左侧的字符。将它们视为变量并将值传递给kill


  • 代码是“你应该有…”以避免这种情况再次发生,对吗?你知道如何杀死由多重处理产生的所有进程吗?-1。不
    kill -9 `ps -ef | grep test.py | grep -v grep | awk '{print $2}'`