Issue
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?
Solution
You can use a subprocess for each function call. The subprocess.run()
method accepts a timeout parameter that is the number of seconds the until the child is killed. From the docs:
The timeout argument is passed to Popen.communicate(). If the timeout expires, the child process will be killed and waited for. The TimeoutExpired exception will be re-raised after the child process has terminated.
So its necessary to handle TimeoutExpired
exceptions that will be thrown if any of the processes do end up exceeding the timeout limit.
Another option is to use signals to set timers and send a SIGLARM
. This has the advantage of being a little less destructive, since you can modify the called functions to properly handle the signal when it comes, giving them a chance to shutdown cleanly. The downside is that signals can be tricky to program, whereas the subprocess option is a pretty self-contained solution right out of the box.
Answered By - Z4-tier
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.