定时多处理功能

use*_*220 6 python timeout

我需要在python函数上设置一个时间限制,它使用一些多处理的东西(我不知道它是否重要).像这样的东西:

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
Run Code Online (Sandbox Code Playgroud)

我环顾网络,我找到了一个超时装饰,但它看起来相当棘手和冗长(我是装饰员的新手).我想要的只是一件简单的事情.

编辑:

我想我太简单了.我的程序遍历上面的函数并将结果存储在如下列表中:

while(something):

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

     # if function timed out then skip

     ris_list.append(retval)
Run Code Online (Sandbox Code Playgroud)

jco*_*ado 11

您应该可以使用此代码执行此操作:

process.join(timeout)
if process.is_alive():
    process.terminate()
Run Code Online (Sandbox Code Playgroud)

因此,不是在函数中设置超时,而是可以加入进程的超时,如果进程在超时后没有完成,则终止它.