如果终止函数python,则获取部分结果
如果函数不能在规定的时间内完成,我将使用多处理停止该函数 这是我的密码:如果终止函数python,则获取部分结果,python,multiprocessing,Python,Multiprocessing,如果函数不能在规定的时间内完成,我将使用多处理停止该函数 这是我的密码: def worker(number): result = [] for i in range(number): x = do_things(i) #this can take a long time result.append(x) return result p = multiprocessing.Process(target=worker, args=
def worker(number):
result = []
for i in range(number):
x = do_things(i) #this can take a long time
result.append(x)
return result
p = multiprocessing.Process(target=worker, args=(random_number,))
p.start()
p.join(10) # Will timeout after 10 seconds
如果还没完成就杀了它
if p.is_alive():
p.terminate()
p.join()
print('killed process because it took too long')
else:
p.join()
print('process finished')
现在,在需要很长时间的函数内部,值被附加到一个列表中,我想让该列表处于它所处的状态,而不管函数是否已完成
4.0奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈4.0奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈奈c,t,I,o,n,,3.0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0,0 0 0 0 0 0 0 0 0,0 0 0 0 0,t,t,0 0 0 0 0,0 0 0,0 0,0 0,0,0 0 0,0,0 0 0,0 0 0,0,0 0,0 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,l,t,h,e,p,r,o,c,e,s,s̶
以下是我尝试的,我指的是共享字典,而不是数组:
def worker(number, dct):
for i in range(number):
x = do_things(i) #this can take a long time
dct[i] = x
dct = {}
p = multiprocessing.Process(target=worker, args=(random_number, dct,))
p.start()
p.join(10) # Will timeout after 10 seconds
if p.is_alive():
p.terminate()
p.join()
print(dct.values())
print('killed process because it took too long')
else:
p.join()
print(dct.values())
print('process finished')
您可以使用
多处理
模块中的共享数组
import multiprocessing
from time import sleep
import random, math
def worker(number, shared_arr):
for i in range(number):
x = do_things(i)
shared_arr[i] = x
def do_things(n):
sleep(random.randint(1,2))
return math.sqrt(n)
arr = multiprocessing.Array('d', 20)
random_number = random.randint(1,20)
p = multiprocessing.Process(target=worker, args=(random_number, arr,))
p.start()
# Will timeout after 10 seconds
p.join(10)
if p.is_alive():
p.terminate()
p.join()
print(arr[:])
print('killed process because it took too long')
else:
p.join()
print(arr[:])
print('process finished')
我稍微修改了你的例子来模拟你的问题
arr
是共享数组,是多处理.array
类型的对象。要初始化它,它需要它要存储的类型(这里是double的'd'
)和大小。由于此处的最大迭代次数为20,因此数组大小也设置为20。该过程将填充函数中的数组,直到其停止,并且生成的数组arr
在该点之前具有非零项,剩余的非计算项为零 您可以使用多处理模块中的共享数组
import multiprocessing
from time import sleep
import random, math
def worker(number, shared_arr):
for i in range(number):
x = do_things(i)
shared_arr[i] = x
def do_things(n):
sleep(random.randint(1,2))
return math.sqrt(n)
arr = multiprocessing.Array('d', 20)
random_number = random.randint(1,20)
p = multiprocessing.Process(target=worker, args=(random_number, arr,))
p.start()
# Will timeout after 10 seconds
p.join(10)
if p.is_alive():
p.terminate()
p.join()
print(arr[:])
print('killed process because it took too long')
else:
p.join()
print(arr[:])
print('process finished')
我稍微修改了你的例子来模拟你的问题arr
是共享数组,是多处理.array
类型的对象。要初始化它,它需要它要存储的类型(这里是double的'd'
)和大小。由于此处的最大迭代次数为20,因此数组大小也设置为20。该过程将填充函数中的数组,直到其停止,并且生成的数组arr
在该点之前具有非零项,剩余的非计算项为零 “我已尝试使用共享数组”您是如何做到这一点的?您正在寻找的答案可能是一个队列,可以在更新的问题中找到。使用您在\uuuuuu main\uuuu
中放入堆栈的dict
将不允许您从另一个进程访问对其所做的更改。每个进程实质上都重新导入\uuuu main\uuuu
,因此它只会得到dct={}
,它是空的。“我尝试过使用共享数组”您是如何做到这一点的?您正在寻找的答案可能是一个队列,并且可以在更新的问题中找到。使用您在\uuuuuu main\uuuu
中放入堆栈的dict
将不允许您从另一个进程访问对其所做的更改。每个进程实质上都重新导入\uuuu main\uuuu
,因此它只获取空的dct={}
。