Timing out a multiprocessing function

放肆的年华 提交于 2019-12-23 09:33:04

问题


I need to set a time limit on a python function which use some multiprocessing stuff (I don't know if it matters). Something like this:

function(a_list):

     p1 = Process(a_list[0:len(a_list/2)])
     p2 = Process(a_list[len(a_list)/2: len(a_list)])

     //start and join p1, p2

I look around the net and I found a time out decorator but it looks quite tricky and verbose (I'm newbie on decorators). What I'd want is a simple thing.

EDIT:

I think I made it too simple. My program iterates over the above function and store result in a list something like this:

while(something):

     retval = function(some_list)  # here I need the time out thing

     # if function timed out then skip

     ris_list.append(retval)

回答1:


You should be able to do that with this code:

process.join(timeout)
if process.is_alive():
    process.terminate()

So instead of setting a timeout in the function, you can join with a timeout the process and if the process hasn't finished after that timeout, then terminate it.



来源:https://stackoverflow.com/questions/8601856/timing-out-a-multiprocessing-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!