Suppose you find yourself in the unfortunate position of having a dependency on a poorly behaved library. Your code needs to call FlakyClient.call(), but sometimes that function ends up hanging for an unacceptable amount of time.
As shown below, one way around this is to wrap the call in its own Process, and use the timeout parameter in the join method to define a maximum amount of time that you're willing to wait on the FlakyClient. This provides a good safeguard, but it also prevents the main body of code from reacting to the result of calling FlakyClient.call(). The only way that I know of addressing this other problem (getting the result into the main body of code) is by using some cumbersome IPC technique.
What is a clean and pythonic way of dealing with these two problems? I want to protect myself if the library call hangs, and be able to use the result if the call completes.
Thanks!
from multiprocessing import Process
from flaky.library import FlakyClient
TIMEOUT_IN_SECS = 10
def make_flaky_call():
result = FlakyClient.call()
proc = Process(target=make_flaky_call)
proc.start()
proc.join(TIMEOUT_IN_SECS)
if proc.is_alive():
proc.terminate()
raise Exception("Timeout during call to FlakyClient.call().")
I cannot speak to Python 2.7, but in Python 3, the correct way to handle this is to make use of asyncio
and the concept of futures.
import concurrent
def make_flaky_call():
return FlakyClient.call()
timeout = 10
with concurrent.futures.ThreadPoolExecutor(max_workers=1) as executor:
future = executor.submit(make_flaky_call) # get a future object
try:
result = await future.result(timeout = timeout)
except concurrent.futures.TimeOutError:
# if a timeout occurs on the call, do something
result = None # default value
This is fairly Pythonic. You can integrate this with the main body of the code. It correctly uses try-except
for error handling. It comes with an inbuilt timeout. It only works in Python 3.5 (thanks to await
- but changing to yield from
makes it compatible with Python 3.4).
For Python 2.7, unfortunately, the right way to handle that is to do what you are currently doing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With