I have the following code where I am trying to call functions with different timeouts. It might happen that the first function times out but the second one could have been executed in the specified time.
import time
from concurrent.futures import ThreadPoolExecutor
def test1(a, b, c):
time.sleep(5)
d=a+b+c
print(d)
def test2(a, b):
time.sleep(5)
d=a+b
print(d)
with ThreadPoolExecutor(max_workers=1) as executor1:
try:
executor1.submit(test1, 1,2,3).result(timeout=1)
except:
executor1.shutdown(wait=False)
print("Pass")
with ThreadPoolExecutor(max_workers=1) as executor2:
try:
executor2.submit(test2, 1,2).result(timeout=8)
except:
executor2.shutdown(wait=False)
print("Pass-2")
Expected Output
Pass
3
Actual Output
Pass
6
3
What I'd like to have is stop the execution of first executor
as soon as there is a timeout
. And continue with the next executor
.