langchain_core.runnables.config.ContextThreadPoolExecutor¶

class langchain_core.runnables.config.ContextThreadPoolExecutor(max_workers=None, thread_name_prefix='', initializer=None, initargs=())[source]¶

ThreadPoolExecutor that copies the context to the child thread.

Initializes a new ThreadPoolExecutor instance.

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

Methods

__init__([max_workers, thread_name_prefix, ...])

Initializes a new ThreadPoolExecutor instance.

map(fn, *iterables[, timeout, chunksize])

Returns an iterator equivalent to map(fn, iter).

shutdown([wait, cancel_futures])

Clean-up the resources associated with the Executor.

submit(func, *args, **kwargs)

Submit a function to the executor.

__init__(max_workers=None, thread_name_prefix='', initializer=None, initargs=())¶

Initializes a new ThreadPoolExecutor instance.

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

map(fn: Callable[[...], T], *iterables: Iterable[Any], timeout: float | None = None, chunksize: int = 1) Iterator[T][source]¶

Returns an iterator equivalent to map(fn, iter).

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

map(func, *iterables) but the calls may be evaluated out-of-order.

Return type

An iterator equivalent to

Raises
  • TimeoutError – If the entire result iterator could not be generated before the given timeout.

  • Exception – If fn(*args) raises for any values.

shutdown(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.

Parameters
  • 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(func: ~typing.Callable[[~P], ~langchain_core.runnables.config.T], *args: ~typing.~P, **kwargs: ~typing.~P) Future[T][source]¶

Submit a function to the executor.

Parameters
  • func (Callable[..., T]) – The function to submit.

  • *args (Any) – The positional arguments to the function.

  • **kwargs (Any) – The keyword arguments to the function.

Returns

The future for the function.

Return type

Future[T]