我有以下情况:
res = []
def longfunc(arg):
# function runs arg number of steps
# each step can take 500 ms to 2 seconds to complete
# longfunc keeps adding result of each step into the array res
def getResult(arg,timeout):
# should call longfunc()
# if longfunc() has not provided result by timeout milliseconds then return None
# if there is partial result in res by timeout milliseconds then return res
# if longfunc() ends before timeout milliseconds then return complete result of longfunc i.e. res array
result = getResult(2, 500)
我正在考虑使用multiprocessing.Process()放入longfunc()一个单独的进程,然后启动另一个线程休眠几timeout毫秒。我无法弄清楚如何在主线程中从它们两个中获得结果并决定哪个先出现。对这种方法或其他方法的任何建议表示赞赏。
函数式编程
一只萌萌小番薯
相关分类