简体   繁体   English

Python multiprocessing.Pool,其中包含崩溃的进程

[英]Python multiprocessing.Pool with processes that crash

Well, they're not supposed to crash, but they do anyway. 好吧,他们不应该崩溃,但无论如何他们都会这样做。 Is there a way to get multiprocessing.Pool, or any other multiprocessing tool to re-start a process that dies? 有没有办法让多处理.Pool或任何其他多处理工具重新启动一个死的进程? How would I do this otherwise? 我怎么会这样做呢?

Thanks! 谢谢!

Edit : Some background. 编辑 :一些背景。 The process does several things with geometry in Autodesk Maya. 该过程使用Autodesk Maya中的几何体执行多项操作。 Which it does totally fine. 它完全没问题。 The problem is that every once in a while I'll have a file that decides, once it's finished and a new scene is being opened, to completely exit Maya (or mayapy) with no python warnings or errors, or critical process errors from Windows. 问题是每隔一段时间我会有一个文件决定,一旦它完成并打开一个新场景,完全退出Maya(或mayapy)没有python警告或错误,或来自Windows的关键进程错误。 It just dies. 它就死了。 There's not really anything I can do about the crashing unfortunately. 不幸的是,我无法做任何关于崩溃的事情。

What I'm hoping for is a way to re-start any processes that have died from a crash. 我希望能够重新启动任何因崩溃而死亡的进程。

Indeed the error handling is better in python 3.3 as masida said. 事实上,正如masida所说,错误处理在python 3.3中更好。 Here I check for timeouts when a child process has died silently. 在这里,我检查一个儿童过程无声地死亡时的超时。

This workaround is for python <3.3 and multiprocessing.pool, of course managing your own processes is a good alternative. 这个解决方法适用于python <3.3和multiprocessing.pool,当然,管理自己的进程是一个不错的选择。

Use pool.map_async to run the processes asynchronously, you can then check if the jobs are done and how long they are taking. 使用pool.map_async以异步方式运行进程,然后可以检查作业是否已完成以及它们需要多长时间。 If they take too long (for instance when one process died and won't return) -> kill all pool processes with pool.terminate() and start over. 如果它们花费的时间太长(例如,当一个进程死亡但不会返回时) - >使用pool.terminate()终止所有池进程并重新开始。 In code: 在代码中:

done = False                                   # not finished yet
while not(done):
     job_start = time.time()                   # start time
     Jobs = pool.map_async(args)               # asynchronous pool call  
     redo = False                              # no redo yet
     while not(Jobs.ready()):                  # while jobs are not finished
       if (time.time() - job_start) > maxWait: # check maximum time (user def.)
           pool.terminate()                    # kill old pool
           pool = multiprocessing.pool(args)       # create new pool
           redo = True                         # redo computation
           break                               # break loop, (not finished)
     if not(redo):                             # computation was successful
         result = Jobs.get()                   # get results 
         done = True                           # exit outer while

Another option is to use a timeout on the iterator returned by pool.imap , which can be provided as a parameter to the iterator's 'next' method, next(timeout). 另一种选择是对pool.imap返回的迭代器使用超时,它可以作为参数提供给迭代器的'next'方法,next(timeout)。 If a process exceeds the timeout, then multiprocessing.TimeoutError is raised in the main process and similar actions as explained above, can follow within the except block, although I have not tested this thoroughly. 如果一个进程超过了超时,那么在主进程中引发multiprocessing.TimeoutError,并且如上所述的类似操作可以在except块中进行,尽管我没有彻底测试过。

Apparently, recently they've changed the behaviour in Python 3.3, to raise an exception in this case: http://hg.python.org/cpython/rev/6d6099f7fe89 显然,最近他们改变了Python 3.3中的行为,在这种情况下引发异常: http//hg.python.org/cpython/rev/6d6099f7fe89

The defect that lead to this ticket is: http://bugs.python.org/issue9205 导致此票证的缺陷是: http//bugs.python.org/issue9205

However, if you manually spawn the workers (which I usually do when I use multiprocessing), you may try to use the Process.is_alive() function: http://docs.python.org/dev/library/multiprocessing#multiprocessing.Process.is_alive 但是,如果手动生成worker(我在使用多处理时通常会这样做),您可以尝试使用Process.is_alive()函数: http//docs.python.org/dev/library/multiprocessing#multiprocessing。 Process.is_alive

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM