Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Individual timeouts for concurrent.futures

I see two ways to specify timeouts in concurrent.futures.

  • as_completed()
  • wait()

Both methods handle N running futures.

I would like to specify an individual timeout for each future.

Use Case:

  • Future for getting data from DB has a timeout of 0.5 secs.
  • Future for getting data from a HTTP server has a timeout of 1.2 secs.

How do I handle this with concurrent.futures? Or is this library not the right tool?

Conclusion

  • AFAIK the solution by mdurant is a good work-around.
  • I think I will use a different library the next time. Maybe asyncio has better support for this. See: https://docs.python.org/3/library/asyncio-task.html#asyncio.sleep
like image 885
guettli Avatar asked Jul 19 '16 10:07

guettli


People also ask

How does concurrent futures ThreadPoolExecutor work?

ThreadPoolExecutor Methods : submit(fn, *args, **kwargs): It runs a callable or a method and returns a Future object representing the execution state of the method. map(fn, *iterables, timeout = None, chunksize = 1) : It maps the method and iterables together immediately and will raise an exception concurrent. futures.

What are concurrent futures?

The concurrent. futures module provides a high-level interface for asynchronously executing callables. The asynchronous execution can be performed with threads, using ThreadPoolExecutor , or separate processes, using ProcessPoolExecutor .

Is concurrent futures thread-safe?

Yes, it's thread-safe.


1 Answers

How about implementing your own:

wait(dbfutures + httpfutures, timeout=0.5)
[fut.cancel() for fut in bdfutures if not fut.done()]
wait(httpfutures, timeout=0.7)
[fut.cancel() for fut in httpfutures if not fut.done()]

(or a while loop with sleep/check or wait with short timeout)

like image 68
mdurant Avatar answered Oct 23 '22 10:10

mdurant