繁体   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