Skip to main content

prefect.task_runners

Classes

TaskRunner

Abstract base class for task runners. A task runner is responsible for submitting tasks to the task run engine running in an execution environment. Submitted tasks are non-blocking and return a future object that can be used to wait for the task to complete and retrieve the result. Task runners are context managers and should be used in a with block to ensure proper cleanup of resources. Methods:

duplicate

Return a new instance of this task runner with the same configuration.

map

Submit multiple tasks to the task run engine. Args:
  • task: The task to submit.
  • parameters: The parameters to use when running the task.
  • wait_for: A list of futures that the task depends on.
Returns:
  • An iterable of future objects that can be used to wait for the tasks to
  • complete and retrieve the results.

name

The name of this task runner

submit

submit

submit

ThreadPoolTaskRunner

A task runner that executes tasks in a separate thread pool. Attributes:
  • max_workers: The maximum number of threads to use for executing tasks. Defaults to PREFECT_TASK_RUNNER_THREAD_POOL_MAX_WORKERS or sys.maxsize.
Examples: Use a thread pool task runner with a flow:
Use a thread pool task runner as a context manager:
Configure max workers via settings:
Methods:

cancel_all

duplicate

map

map

map

submit

submit

submit

Submit a task to the task run engine running in a separate thread. Args:
  • task: The task to submit.
  • parameters: The parameters to use when running the task.
  • wait_for: A list of futures that the task depends on.
Returns:
  • A future object that can be used to wait for the task to complete and
  • retrieve the result.

ProcessPoolTaskRunner

A task runner that executes tasks in a separate process pool. This task runner uses ProcessPoolExecutor to run tasks in separate processes, providing true parallelism for CPU-bound tasks and process isolation. Tasks are executed with proper context propagation and error handling. Attributes:
  • max_workers: The maximum number of processes to use for executing tasks. Defaults to multiprocessing.cpu_count() if PREFECT_TASKS_RUNNER_PROCESS_POOL_MAX_WORKERS is not set.
Examples: Use a process pool task runner with a flow:
Use a process pool task runner as a context manager:
Configure max workers via settings:
Methods:

cancel_all

duplicate

map

map

map

set_subprocess_message_processor_factories

submit

submit

submit

Submit a task to the task run engine running in a separate process. Args:
  • task: The task to submit.
  • parameters: The parameters to use when running the task.
  • wait_for: A list of futures that the task depends on.
  • dependencies: A dictionary of dependencies for the task.
Returns:
  • A future object that can be used to wait for the task to complete and
  • retrieve the result.

subprocess_message_processor_factories

subprocess_message_processor_factories

PrefectTaskRunner

Methods:

duplicate

map

map

map

submit

submit

submit

Submit a task to the task run engine running in a separate thread. Args:
  • task: The task to submit.
  • parameters: The parameters to use when running the task.
  • wait_for: A list of futures that the task depends on.
Returns:
  • A future object that can be used to wait for the task to complete and
  • retrieve the result.