Cancellation of futures: f_nocancel, CancelOnShutdownExecutor

Futures API

more_executors.f_nocancel(future)

Wrap a future to block cancellation.

Signature: Future<X> Future<X>

Parameters

future (Future) – Any future.

Returns

Future

A wrapped version of future which cannot be cancelled.

New in version 1.19.0.

Executors API

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

An executor which delegates to another executor and cancels all futures when the executor is shut down.

This class is useful in conjunction with executors having custom cancel behavior, such as PollExecutor.

Note

From Python 3.9 onwards, the standard shutdown() method includes a cancel_futures parameter which can be used to cancel futures on shutdown.

These two approaches of cancelling futures on shutdown have the following differences:

  • cancel_futures=True will only cancel futures which have not yet started running.

  • CancelOnShutdownExecutor will attempt to cancel all incomplete futures, including those which are running.

If you’re using other executors from this library and you want to ensure futures are cancelled on shutdown, CancelOnShutdownExecutor should be preferred because several of the executor classes in this library do support cancellation of running futures (unlike the executors in the standard library).

Parameters
  • delegate (Executor) – executor to which callables will be submitted

  • 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.

shutdown(wait=True, **_kwargs)

Shut down the executor.

All futures created by this executor which have not yet been completed will have cancel() invoked.

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

classmethod Executors.with_cancel_on_shutdown(executor, logger=None)
Returns

a new executor which attempts to cancel any pending futures when the executor is shut down.

Return type

CancelOnShutdownExecutor