简体   繁体   English

Python多处理导致许多僵尸进程

[英]Python Multiprocessing leading to many zombie processes

I have been implementing python's multiprocessing library using a pool of workers. 我一直在使用工作池来实现python的多处理库。 I implemented the following code 我实现了以下代码

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)

However, after running the process for a while, I get lot of running background processes of my app. 但是,在运行该过程一段时间后,我的应用程序运行后台进程很多。 Here is a snapshot after doing ps aux for my app 这是为我的应用程序做ps aux之后的快照

显示所有僵尸进程的快照

Now, I have read a lot of similar questions on stackoverflow like how to kill zombie processes created by multiprocessing module? 现在,我已经在stackoverflow上阅读了很多类似的问题,比如如何杀死多处理模块创建的僵尸进程? which calls for using .join() which I have already implemented and I learned how to kill all these processes from here Python Multiprocessing Kill Processes . 它要求使用我已经实现的.join(),并且我从这里学习了如何从Python Multiprocessing Kill Processes中杀死所有这些进程 But I want to know what possibly could go wrong with my code. 但我想知道我的代码可能出错的地方。 I won't able to share all of my code in the main1 function but I have put the entire code block in try catch block to avoid cases where an error in the main code could lead to zombie 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

I'm still very new to the concept of parallel programming and debugging issues with it is turning out to be tricky.Any help will be greatly appreciated. 我对并行编程和调试问题的概念还很陌生,结果很棘手。非常感谢任何帮助。

Usually the most common problem is that the pool is created but it is not closed. 通常最常见的问题是池已创建但未关闭。

The best way I know to guarantee that the pool is closed is to use a try/finally clause: 我知道保证池关闭的最好方法是使用try / finally子句:

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

If you don't want to struggle with multiprocessing , I wrote a simple package named parmap that wraps multiprocessing to make my life (and potentially yours) easier. 如果你不想为multiprocessing parmap ,我写了一个名为parmap的简单包,它包含了多处理,使我的生活(以及可能你的生活)更容易。

pip install parmap

import parmap
parmap.map(yourfunction, arguments)

From the parmap usage section: 从parmap用法部分:

  • Simple parallel example: 简单的并行示例:

     import parmap y1 = [myfunction(x, argument1, argument2) for x in mylist] y2 = parmap.map(myfunction, mylist, argument1, argument2) y1 == y2 
  • Iterating over a list of tuples: 迭代元组列表:

     # 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