How can I skip and continue when a step of a loop takes too long?
Below are a few desirable interfaces I'd like:
for x in some_collection:
with Timeout(2.5): # time out after 2.5 seconds
do_something(x)
Would be nice (and even better -- but might be harder):
with TimeoutLoopStep(2.5): # time out after 2.5 seconds
for x in some_collection:
do_something(x)
Possibly an equivalent timeout_loop_step
decorator to consume iterators in that manner:
timeout_loop_step(2.5)(map(do_something, some_collection))
--- Addendum (edit) to dispel some ambiguity ---
What I'm looking for is a context manager and/or decorator that will interrupt the processing of a step of an iteration if it lasts too long.
Most APIs that talk to a remote system (e.g. DBs) provide some sort of timeout
parameter that means "do this, but if you exceed timeout
trying, just phorgetaboutit and move on to the next item..."
But if I'm not offered such control, I'd like to achieve it, externally, with a reusable context manager or decorator.