I would give up the most obvious answer - using signal.alarm () and an alarm handler that asynchronously throws an exception to jump out of the task. Theoretically, this should work fine, but in practice, the cPython interpreter code does not guarantee that the handler runs within the required time interval. Signal processing can be delayed by x by the number of bytecode instructions, so an exception can still be raised after an explicit rejection of the buffer (outside the context of the try block).
The problem we encountered regularly was that an exception to the alarm handler would be raised after the timecode was completed.
Since there is not much flow control, I relied on process control to handle tasks that need to timeout. Essentially, the bottom line is to pass the task to the child process and kill the child process if the task takes too long. multiprocessing.pool is not quite that complicated - so I have a class consisting of two rows for this level of control.
source share