Throttling: ThrottleExecutor

ThrottleExecutor limits the number of concurrently executing futures.

class more_executors.ThrottleExecutor(delegate, count, logger=None, name='default', block=False)

An executor which delegates to another executor while enforcing a limit on the number of futures running concurrently.

  • Callables are submitted to the delegate executor, from a different thread than the calling thread.

  • Where count is used to initialize this executor, if there are already count futures submitted to the delegate executor and not yet done(), additional callables will either be queued or will block on submit, and will only be submitted to the delegate executor once there are less than count futures in progress.

New in version 1.9.0.

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

  • count (int, callable) –

    int:

    maximum number of concurrently running futures.

    callable:

    a callable which returns an int (or None, to indicate no throttling).

    The callable will be invoked each time this executor needs to decide whether to throttle futures; this may be used to implement dynamic throttling.

    New in version 2.5.0.

  • block (bool) –

    If True, calls to submit() on this executor may block if there are already count futures in progress.

    Otherwise, calls to submit() will always return immediately and callables will be queued internally.

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

Changed in version 2.11.0: Introduced block.

classmethod Executors.with_throttle(executor, count, logger=None, block=False)
Returns

a new executor which enforces a limit on the number of concurrently pending futures.

Return type

ThrottleExecutor

New in version 1.9.0.