11 ms·
How (not) to set a timeout on a computation in Python
- z0r 15y agoWe use this method where I work :) It's true that if the thread hasn't finished by the end of the time limit, an exception will be raised and the thread will keep running. Part of our solution is that the thread is also aware of the time limit. It checks to see if it has timed out at regular intervals during its calculations, and quickly exits itself if it has expired. It's not the prettiest thing on earth, but it gets the job done for us
- robfig 15y agoIsn't the usual way to do this is to just pass down a "timeout" parameter and have the method take care of time limiting itself? That always seemed to work well for me. (And APIs provide such an interface if they have long-running blocking methods)
- Dylan16807 15y agoThe article dismisses setting an exception in the same line it's mentioned but it seems to me like a good option. As long as it's a python function then any incidental c calls will quickly return and let it abort, right? The difficulty of stopping a c function is a whole different ballgame.
- eliben 15y agoThe problem is providing a generic solution that will work for all kinds of functions. Much Python code uses C calls under the hood, so this isn't easy to do.
- yaroslavvb 15y agoAnother problem with SIGALARM is that there's only one of them, so setting second alarm will reset the previous counter, and make the system try to run both handlers at the same time on trigger
- mkramlich 15y agoShotgun solution: make the target fn be a process. Have a 2nd process be a watchdog and kill the target process if it exceeds the time limit. Design the arch of this system such that no matter what the target proc was doing precisely when killed, that the system can recover/resume reasonably well.
- DrJ 15y agoIn this case, if a timeout is required, it’s recommended to use asynchronous IO which naturally supports interruptions. Unfortunately, asynchronous IO also makes code more convoluted and difficult to write. if you are bad at Async Coding ;)