I have three functions f1(x), f2(x) and f3(x) returning a result (imagine API calls, no control over the functions). Those functions can take an undetermined amount of time to end when called (but usually under 1s). Now I need to call all those functions and select the best result but under the constrain that I need to return some result in max t miliseconds. Pseudocode:
def select_result(x, max_t):
r1 = f1(x) # but stop if it takes longer than max_t miliseconds
r2 = f2(x) # but stop if it takes longer than max_t miliseconds
r3 = f3(x) # but stop if it takes longer than max_t miliseconds
return max(r1, r2, r3) # or None if it took too long
How can I achieve that in Python?