I want to return already completed task values in multiprocessing for a given timeout after killing all the ongoing and queued tasks.
For example, the following function needed to be run parallelly using pool.starmap()
For values, 1 to 100.
def func(x):
time.sleep(1)
return x * 2
Let's say, after 5 seconds (defined timeout), tasks related to values 1 to 10 are completed and 11-90 are still running or queued. Then at 5 seconds, I want to kill all processes and return the values of the completed tasks for the values 1 to 10. So then the return list for the 100 tasks should be [2,4,6,8,10, ....18, 20, None, None,....None]. The main function I tried is as follows,
def main():
pool = multiprocessing.Pool(processes=3)
val = [[i] for i in range(100)]
result_obj = pool.starmap_async(func, val)
result_obj.wait(5)
if result_obj.ready():
result = result_obj.get(1)
However, here the processes are still running in the background and couldn't find a way to kill those processes and capture the already completed tasks after 5 seconds. Is this possible?