Timeouts in python and why you should use python 3
I started using python 3 over python 2 for the last three years, and I think you should do it too if you have not done yet. First python 2.7 end-of-life date is in 2020, and there will not be a python 2.81. Second, python 3 syntax improvements and new APIs are so much more helpful and practical, that so far it has to be worth the effort of learning python 3. One example that comes to my mind is when implementing timeouts when making a system call or when calling a function within python interpreter.
Let’s start first with the case of a system call. An example of how to implement a system call protected by a timeout in python 2 can be found below.
The implementation of this example plus some more comments about its implementation can be found in this code at my exercise repo.
The equivalent example in python 3 is much simpler, and it is shown below.
As you can see, in python 3, the timeout is now a native functionality of the new API. Now, you might think this is not such a big deal. However, inappropriate timeout implementation opens the possibility of leaving dangling subprocesses in some edge cases when exceptions are raised. If your program runs for a long time, this could leak processes used when for executing each command, eventually depleting host resources.
Another nontrivial implementation is adding a timeout for a function called within the python interpreter. This issue created several discussions in different forums2, and it was also the inspiration for small timeout library3. However, using new python 3 concurrent.futures API, it is possible to create a simple function timeout because the timeout functionality is again a native functionality of the API.
For example, if you would like to make a timeout decorator, you can write something like it is shown below.
Now, you can now add a timeout by simply decorating the function of interest:
for fully functional example can be found in my example repo.
It is also possible to decorate member functions with a timeout that collects the waiting time from the object itself. Take a look at this decorator below.
You can use this in the following way:
For fully functional example can be found in my example repo.
So there you have it, a few examples of improved python 3 API that I found particularly useful. In the era of distributed applications and microservices, it is fundamental to protect your code from unexpected behavior using timeouts.
References
Note of a interdiciplinarian by Victor E. Bazterra is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License.