Timing out futures: f_timeout, TimeoutExecutor

Futures API

more_executors.f_timeout(future, timeout)

Wrap a future to cancel it after a timeout is reached.

Signature: Future<X>, float Future<X>

Parameters
  • future (Future) – Any future.

  • timeout (float) – A timeout to apply to the future, in seconds.

Returns

Future

A wrapped version of future which may be cancelled if the future has not completed within timeout seconds.

Note: only a single attempt is made to cancel the future, and there is no guarantee that the cancel will succeed.

New in version 1.19.0.

Executors API

class more_executors.TimeoutExecutor(delegate, timeout, logger=None, name='default')

An executor which delegates to another executor while applying a timeout to each returned future.

For any futures returned by this executor, if the future hasn’t completed approximately within timeout seconds of its creation, an attempt will be made to cancel the future.

Note that only a single attempt is made to cancel any future, and there is no guarantee that this will succeed.

New in version 1.7.0.

Parameters
  • delegate (Executor) – an executor to which callables are submitted

  • timeout (float) – timeout (in seconds) after which concurrent.futures.Future.cancel() will be invoked on any generated future which has not completed

  • logger (Logger) – a logger used for messages from this executor

  • name (str) – a name for this executor

Changed in version 2.7.0: Introduced name.

submit_timeout(timeout, fn, *args, **kwargs)

Like submit(fn, *args, **kwargs), but uses the specified timeout rather than this executor’s default.

New in version 1.19.0.

classmethod Executors.with_timeout(executor, timeout, logger=None)
Returns

a new executor which will attempt to cancel any futures if they’ve not completed within the given timeout.

Return type

TimeoutExecutor

New in version 1.7.0.