Call a function with a timeout.
Additional arguments and keyword arguments provided are passed to the
function. This will re-raise any exceptions raised by the function.
If a timeout expires, but the function returns before the next pass in
the event loop, then the timeout will be diffused.
If a coroutine is already scheduled to run (such as if it received a
kevent), and the timeout expires, the timeout will be put on "hold" to
let the coroutine run and process the data. If the function returns,
then the timeout will be defused, otherwise the timeout will be given
another chance to fire during the next pass through the event loop. One
should note that due to this behavior, if a coroutine is continually
receiving kevents, the timeout will never fire until the kevents stop.
Nested timeouts will be handled correctly. If an outer timeout fires
first, then only the outer except TimeoutError exception handler
will catch it. An exception handlers on the inside will be skipped
becaue the actual exception is the Interrupted exception until it
gets to the original with_timeout frame.
Nested timeouts that are set to fire at the exact same time are not
defined which one will fire first.
Care must be taken to never catch the Interrupted exception within
code that is wrapped with a timeout.
- Returns:
- Returns the return value of the function.
- Raises:
TimeoutError - : The function did not return within the specified
timeout.
|