I want to run numerous functions using Pool.map()
from the Python multiprocessing
module that i can interrupt (for example using ctrl+C
).
I can’t find a way that I can do this and retain the data of already completed function evaluation while skipping the pending results. Whether the currently running processes finish or get terminated is not really important.
I have tried the following when interrupting with KeyboardInterrupt
, but output
would not be defined in this case.
from multiprocessing import Pool
from time import sleep
def f(i):
sleep(i)
return i
with Pool() as p:
try:
output = p.map(f, range(10))
except KeyboardInterrupt:
p.close()
print(output)