concurrent.futures API Reference

Full documentation for concurrent.futures (Standard Library / Native)

concurrent.futures.ALL_COMPLETED
Error rendering doc: No Python documentation found for 'ALL_COMPLETED'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.
concurrent.futures.BrokenExecutor
Python Library Documentation: class BrokenExecutor in module concurrent.futures._base

class BrokenExecutor(builtins.RuntimeError)
 |  Raised when a executor has become non-functional after a severe failure.
 |
 |  Method resolution order:
 |      BrokenExecutor
 |      builtins.RuntimeError
 |      builtins.Exception
 |      builtins.BaseException
 |      builtins.object
 |
 |  Data descriptors defined here:
 |
 |  __weakref__
 |      list of weak references to the object
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from builtins.RuntimeError:
 |
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Static methods inherited from builtins.RuntimeError:
 |
 |  __new__(*args, **kwargs) class method of builtins.RuntimeError
 |      Create and return a new object.  See help(type) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from builtins.BaseException:
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __reduce__(...)
 |      Helper for pickle.
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __setstate__(...)
 |
 |  __str__(self, /)
 |      Return str(self).
 |
 |  add_note(...)
 |      Exception.add_note(note) --
 |      add a note to the exception
 |
 |  with_traceback(...)
 |      Exception.with_traceback(tb) --
 |      set self.__traceback__ to tb and return self.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from builtins.BaseException:
 |
 |  __cause__
 |      exception cause
 |
 |  __context__
 |      exception context
 |
 |  __dict__
 |
 |  __suppress_context__
 |
 |  __traceback__
 |
 |  args
concurrent.futures.CancelledError
Python Library Documentation: class CancelledError in module concurrent.futures._base

class CancelledError(Error)
 |  The Future was cancelled.
 |
 |  Method resolution order:
 |      CancelledError
 |      Error
 |      builtins.Exception
 |      builtins.BaseException
 |      builtins.object
 |
 |  Data descriptors inherited from Error:
 |
 |  __weakref__
 |      list of weak references to the object
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from builtins.Exception:
 |
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Static methods inherited from builtins.Exception:
 |
 |  __new__(*args, **kwargs) class method of builtins.Exception
 |      Create and return a new object.  See help(type) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from builtins.BaseException:
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __reduce__(...)
 |      Helper for pickle.
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __setstate__(...)
 |
 |  __str__(self, /)
 |      Return str(self).
 |
 |  add_note(...)
 |      Exception.add_note(note) --
 |      add a note to the exception
 |
 |  with_traceback(...)
 |      Exception.with_traceback(tb) --
 |      set self.__traceback__ to tb and return self.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from builtins.BaseException:
 |
 |  __cause__
 |      exception cause
 |
 |  __context__
 |      exception context
 |
 |  __dict__
 |
 |  __suppress_context__
 |
 |  __traceback__
 |
 |  args
concurrent.futures.Executor
Python Library Documentation: class Executor in module concurrent.futures._base

class Executor(builtins.object)
 |  This is an abstract base class for concrete asynchronous executors.
 |
 |  Methods defined here:
 |
 |  __enter__(self)
 |
 |  __exit__(self, exc_type, exc_val, exc_tb)
 |
 |  map(self, fn, *iterables, timeout=None, chunksize=1)
 |      Returns an iterator equivalent to map(fn, iter).
 |
 |      Args:
 |          fn: A callable that will take as many arguments as there are
 |              passed iterables.
 |          timeout: The maximum number of seconds to wait. If None, then there
 |              is no limit on the wait time.
 |          chunksize: The size of the chunks the iterable will be broken into
 |              before being passed to a child process. This argument is only
 |              used by ProcessPoolExecutor; it is ignored by
 |              ThreadPoolExecutor.
 |
 |      Returns:
 |          An iterator equivalent to: map(func, *iterables) but the calls may
 |          be evaluated out-of-order.
 |
 |      Raises:
 |          TimeoutError: If the entire result iterator could not be generated
 |              before the given timeout.
 |          Exception: If fn(*args) raises for any values.
 |
 |  shutdown(self, wait=True, *, cancel_futures=False)
 |      Clean-up the resources associated with the Executor.
 |
 |      It is safe to call this method several times. Otherwise, no other
 |      methods can be called after this one.
 |
 |      Args:
 |          wait: If True then shutdown will not return until all running
 |              futures have finished executing and the resources used by the
 |              executor have been reclaimed.
 |          cancel_futures: If True then shutdown will cancel all pending
 |              futures. Futures that are completed or running will not be
 |              cancelled.
 |
 |  submit(self, fn, /, *args, **kwargs)
 |      Submits a callable to be executed with the given arguments.
 |
 |      Schedules the callable to be executed as fn(*args, **kwargs) and returns
 |      a Future instance representing the execution of the callable.
 |
 |      Returns:
 |          A Future representing the given call.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables
 |
 |  __weakref__
 |      list of weak references to the object
concurrent.futures.FIRST_COMPLETED
Error rendering doc: No Python documentation found for 'FIRST_COMPLETED'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.
concurrent.futures.FIRST_EXCEPTION
Error rendering doc: No Python documentation found for 'FIRST_EXCEPTION'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.
concurrent.futures.Future
Python Library Documentation: class Future in module concurrent.futures._base

class Future(builtins.object)
 |  Represents the result of an asynchronous computation.
 |
 |  Methods defined here:
 |
 |  __init__(self)
 |      Initializes the future. Should not be called by clients.
 |
 |  __repr__(self)
 |      Return repr(self).
 |
 |  add_done_callback(self, fn)
 |      Attaches a callable that will be called when the future finishes.
 |
 |      Args:
 |          fn: A callable that will be called with this future as its only
 |              argument when the future completes or is cancelled. The callable
 |              will always be called by a thread in the same process in which
 |              it was added. If the future has already completed or been
 |              cancelled then the callable will be called immediately. These
 |              callables are called in the order that they were added.
 |
 |  cancel(self)
 |      Cancel the future if possible.
 |
 |      Returns True if the future was cancelled, False otherwise. A future
 |      cannot be cancelled if it is running or has already completed.
 |
 |  cancelled(self)
 |      Return True if the future was cancelled.
 |
 |  done(self)
 |      Return True if the future was cancelled or finished executing.
 |
 |  exception(self, timeout=None)
 |      Return the exception raised by the call that the future represents.
 |
 |      Args:
 |          timeout: The number of seconds to wait for the exception if the
 |              future isn't done. If None, then there is no limit on the wait
 |              time.
 |
 |      Returns:
 |          The exception raised by the call that the future represents or None
 |          if the call completed without raising.
 |
 |      Raises:
 |          CancelledError: If the future was cancelled.
 |          TimeoutError: If the future didn't finish executing before the given
 |              timeout.
 |
 |  result(self, timeout=None)
 |      Return the result of the call that the future represents.
 |
 |      Args:
 |          timeout: The number of seconds to wait for the result if the future
 |              isn't done. If None, then there is no limit on the wait time.
 |
 |      Returns:
 |          The result of the call that the future represents.
 |
 |      Raises:
 |          CancelledError: If the future was cancelled.
 |          TimeoutError: If the future didn't finish executing before the given
 |              timeout.
 |          Exception: If the call raised then that exception will be raised.
 |
 |  running(self)
 |      Return True if the future is currently executing.
 |
 |  set_exception(self, exception)
 |      Sets the result of the future as being the given exception.
 |
 |      Should only be used by Executor implementations and unit tests.
 |
 |  set_result(self, result)
 |      Sets the return value of work associated with the future.
 |
 |      Should only be used by Executor implementations and unit tests.
 |
 |  set_running_or_notify_cancel(self)
 |      Mark the future as running or process any cancel notifications.
 |
 |      Should only be used by Executor implementations and unit tests.
 |
 |      If the future has been cancelled (cancel() was called and returned
 |      True) then any threads waiting on the future completing (though calls
 |      to as_completed() or wait()) are notified and False is returned.
 |
 |      If the future was not cancelled then it is put in the running state
 |      (future calls to running() will return True) and True is returned.
 |
 |      This method should be called by Executor implementations before
 |      executing the work associated with this future. If this method returns
 |      False then the work should not be executed.
 |
 |      Returns:
 |          False if the Future was cancelled, True otherwise.
 |
 |      Raises:
 |          RuntimeError: if this method was already called or if set_result()
 |              or set_exception() was called.
 |
 |  ----------------------------------------------------------------------
 |  Class methods defined here:
 |
 |  __class_getitem__ = GenericAlias(...)
 |      Represent a PEP 585 generic type
 |
 |      E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |
 |  __dict__
 |      dictionary for instance variables
 |
 |  __weakref__
 |      list of weak references to the object
concurrent.futures.InvalidStateError
Python Library Documentation: class InvalidStateError in module concurrent.futures._base

class InvalidStateError(Error)
 |  The operation is not allowed in this state.
 |
 |  Method resolution order:
 |      InvalidStateError
 |      Error
 |      builtins.Exception
 |      builtins.BaseException
 |      builtins.object
 |
 |  Data descriptors inherited from Error:
 |
 |  __weakref__
 |      list of weak references to the object
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from builtins.Exception:
 |
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Static methods inherited from builtins.Exception:
 |
 |  __new__(*args, **kwargs) class method of builtins.Exception
 |      Create and return a new object.  See help(type) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from builtins.BaseException:
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __reduce__(...)
 |      Helper for pickle.
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __setstate__(...)
 |
 |  __str__(self, /)
 |      Return str(self).
 |
 |  add_note(...)
 |      Exception.add_note(note) --
 |      add a note to the exception
 |
 |  with_traceback(...)
 |      Exception.with_traceback(tb) --
 |      set self.__traceback__ to tb and return self.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from builtins.BaseException:
 |
 |  __cause__
 |      exception cause
 |
 |  __context__
 |      exception context
 |
 |  __dict__
 |
 |  __suppress_context__
 |
 |  __traceback__
 |
 |  args
concurrent.futures.ProcessPoolExecutor
Python Library Documentation: class ProcessPoolExecutor in module concurrent.futures.process

class ProcessPoolExecutor(concurrent.futures._base.Executor)
 |  ProcessPoolExecutor(max_workers=None, mp_context=None, initializer=None, initargs=(), *, max_tasks_per_child=None)
 |
 |  Method resolution order:
 |      ProcessPoolExecutor
 |      concurrent.futures._base.Executor
 |      builtins.object
 |
 |  Methods defined here:
 |
 |  __init__(self, max_workers=None, mp_context=None, initializer=None, initargs=(), *, max_tasks_per_child=None)
 |      Initializes a new ProcessPoolExecutor instance.
 |
 |      Args:
 |          max_workers: The maximum number of processes that can be used to
 |              execute the given calls. If None or not given then as many
 |              worker processes will be created as the machine has processors.
 |          mp_context: A multiprocessing context to launch the workers created
 |              using the multiprocessing.get_context('start method') API. This
 |              object should provide SimpleQueue, Queue and Process.
 |          initializer: A callable used to initialize worker processes.
 |          initargs: A tuple of arguments to pass to the initializer.
 |          max_tasks_per_child: The maximum number of tasks a worker process
 |              can complete before it will exit and be replaced with a fresh
 |              worker process. The default of None means worker process will
 |              live as long as the executor. Requires a non-'fork' mp_context
 |              start method. When given, we default to using 'spawn' if no
 |              mp_context is supplied.
 |
 |  map(self, fn, *iterables, timeout=None, chunksize=1)
 |      Returns an iterator equivalent to map(fn, iter).
 |
 |      Args:
 |          fn: A callable that will take as many arguments as there are
 |              passed iterables.
 |          timeout: The maximum number of seconds to wait. If None, then there
 |              is no limit on the wait time.
 |          chunksize: If greater than one, the iterables will be chopped into
 |              chunks of size chunksize and submitted to the process pool.
 |              If set to one, the items in the list will be sent one at a time.
 |
 |      Returns:
 |          An iterator equivalent to: map(func, *iterables) but the calls may
 |          be evaluated out-of-order.
 |
 |      Raises:
 |          TimeoutError: If the entire result iterator could not be generated
 |              before the given timeout.
 |          Exception: If fn(*args) raises for any values.
 |
 |  shutdown(self, wait=True, *, cancel_futures=False)
 |      Clean-up the resources associated with the Executor.
 |
 |      It is safe to call this method several times. Otherwise, no other
 |      methods can be called after this one.
 |
 |      Args:
 |          wait: If True then shutdown will not return until all running
 |              futures have finished executing and the resources used by the
 |              executor have been reclaimed.
 |          cancel_futures: If True then shutdown will cancel all pending
 |              futures. Futures that are completed or running will not be
 |              cancelled.
 |
 |  submit(self, fn, /, *args, **kwargs)
 |      Submits a callable to be executed with the given arguments.
 |
 |      Schedules the callable to be executed as fn(*args, **kwargs) and returns
 |      a Future instance representing the execution of the callable.
 |
 |      Returns:
 |          A Future representing the given call.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from concurrent.futures._base.Executor:
 |
 |  __enter__(self)
 |
 |  __exit__(self, exc_type, exc_val, exc_tb)
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from concurrent.futures._base.Executor:
 |
 |  __dict__
 |      dictionary for instance variables
 |
 |  __weakref__
 |      list of weak references to the object
concurrent.futures.ThreadPoolExecutor
Python Library Documentation: class ThreadPoolExecutor in module concurrent.futures.thread

class ThreadPoolExecutor(concurrent.futures._base.Executor)
 |  ThreadPoolExecutor(max_workers=None, thread_name_prefix='', initializer=None, initargs=())
 |
 |  Method resolution order:
 |      ThreadPoolExecutor
 |      concurrent.futures._base.Executor
 |      builtins.object
 |
 |  Methods defined here:
 |
 |  __init__(self, max_workers=None, thread_name_prefix='', initializer=None, initargs=())
 |      Initializes a new ThreadPoolExecutor instance.
 |
 |      Args:
 |          max_workers: The maximum number of threads that can be used to
 |              execute the given calls.
 |          thread_name_prefix: An optional name prefix to give our threads.
 |          initializer: A callable used to initialize worker threads.
 |          initargs: A tuple of arguments to pass to the initializer.
 |
 |  shutdown(self, wait=True, *, cancel_futures=False)
 |      Clean-up the resources associated with the Executor.
 |
 |      It is safe to call this method several times. Otherwise, no other
 |      methods can be called after this one.
 |
 |      Args:
 |          wait: If True then shutdown will not return until all running
 |              futures have finished executing and the resources used by the
 |              executor have been reclaimed.
 |          cancel_futures: If True then shutdown will cancel all pending
 |              futures. Futures that are completed or running will not be
 |              cancelled.
 |
 |  submit(self, fn, /, *args, **kwargs)
 |      Submits a callable to be executed with the given arguments.
 |
 |      Schedules the callable to be executed as fn(*args, **kwargs) and returns
 |      a Future instance representing the execution of the callable.
 |
 |      Returns:
 |          A Future representing the given call.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from concurrent.futures._base.Executor:
 |
 |  __enter__(self)
 |
 |  __exit__(self, exc_type, exc_val, exc_tb)
 |
 |  map(self, fn, *iterables, timeout=None, chunksize=1)
 |      Returns an iterator equivalent to map(fn, iter).
 |
 |      Args:
 |          fn: A callable that will take as many arguments as there are
 |              passed iterables.
 |          timeout: The maximum number of seconds to wait. If None, then there
 |              is no limit on the wait time.
 |          chunksize: The size of the chunks the iterable will be broken into
 |              before being passed to a child process. This argument is only
 |              used by ProcessPoolExecutor; it is ignored by
 |              ThreadPoolExecutor.
 |
 |      Returns:
 |          An iterator equivalent to: map(func, *iterables) but the calls may
 |          be evaluated out-of-order.
 |
 |      Raises:
 |          TimeoutError: If the entire result iterator could not be generated
 |              before the given timeout.
 |          Exception: If fn(*args) raises for any values.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from concurrent.futures._base.Executor:
 |
 |  __dict__
 |      dictionary for instance variables
 |
 |  __weakref__
 |      list of weak references to the object
concurrent.futures.TimeoutError
Python Library Documentation: class TimeoutError in module builtins

class TimeoutError(OSError)
 |  Timeout expired.
 |
 |  Method resolution order:
 |      TimeoutError
 |      OSError
 |      Exception
 |      BaseException
 |      object
 |
 |  Methods defined here:
 |
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from OSError:
 |
 |  __reduce__(...)
 |      Helper for pickle.
 |
 |  __str__(self, /)
 |      Return str(self).
 |
 |  ----------------------------------------------------------------------
 |  Static methods inherited from OSError:
 |
 |  __new__(*args, **kwargs) class method of builtins.OSError
 |      Create and return a new object.  See help(type) for accurate signature.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from OSError:
 |
 |  characters_written
 |
 |  errno
 |      POSIX exception code
 |
 |  filename
 |      exception filename
 |
 |  filename2
 |      second exception filename
 |
 |  strerror
 |      exception strerror
 |
 |  winerror
 |      Win32 exception code
 |
 |  ----------------------------------------------------------------------
 |  Methods inherited from BaseException:
 |
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |
 |  __repr__(self, /)
 |      Return repr(self).
 |
 |  __setstate__(...)
 |
 |  add_note(...)
 |      Exception.add_note(note) --
 |      add a note to the exception
 |
 |  with_traceback(...)
 |      Exception.with_traceback(tb) --
 |      set self.__traceback__ to tb and return self.
 |
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from BaseException:
 |
 |  __cause__
 |      exception cause
 |
 |  __context__
 |      exception context
 |
 |  __dict__
 |
 |  __suppress_context__
 |
 |  __traceback__
 |
 |  args
concurrent.futures.as_completed
Python Library Documentation: function as_completed in module concurrent.futures._base

as_completed(fs, timeout=None)
    An iterator over the given futures that yields each as it completes.

    Args:
        fs: The sequence of Futures (possibly created by different Executors) to
            iterate over.
        timeout: The maximum number of seconds to wait. If None, then there
            is no limit on the wait time.

    Returns:
        An iterator that yields the given Futures as they complete (finished or
        cancelled). If any given Futures are duplicated, they will be returned
        once.

    Raises:
        TimeoutError: If the entire result iterator could not be generated
            before the given timeout.
concurrent.futures.wait
Python Library Documentation: function wait in module concurrent.futures._base

wait(fs, timeout=None, return_when='ALL_COMPLETED')
    Wait for the futures in the given sequence to complete.

    Args:
        fs: The sequence of Futures (possibly created by different Executors) to
            wait upon.
        timeout: The maximum number of seconds to wait. If None, then there
            is no limit on the wait time.
        return_when: Indicates when this function should return. The options
            are:

            FIRST_COMPLETED - Return when any future finishes or is
                              cancelled.
            FIRST_EXCEPTION - Return when any future finishes by raising an
                              exception. If no future raises an exception
                              then it is equivalent to ALL_COMPLETED.
            ALL_COMPLETED -   Return when all futures finish or are cancelled.

    Returns:
        A named 2-tuple of sets. The first set, named 'done', contains the
        futures that completed (is finished or cancelled) before the wait
        completed. The second set, named 'not_done', contains uncompleted
        futures. Duplicate futures given to *fs* are removed and will be
        returned only once.