def __call__(self, *args, **kargs):
def function_process(pipe, function, args, kargs):
pipe.send(function(*args, **kargs)) # A: result got from function process
p_pipe, c_pipe = multiprocessing.Pipe()
p = Process(target=function_process, args=(c_pipe, self.function, args, kargs))
p.start()
p.join(self._timeout_threshold) # Wait
if p.exception:
# if there is other Error
raise RuntimeError
elif p.is_alive():
# if passes the timeout threshold, terminate function process
p.terminate()
raise TimeoutError('Timeout')
else:
return p_pipe.recv() # return result from A
评论列表
文章目录