Skip to main content

Define a workflow

Make a Python function a workflow by adding the @flow decorator to it:
Run the workflow by calling it like a normal Python function:
You can create a workflow from a method on a class:
or from a generator function:
You can view all workflow runs in the Prefect UI.

Create tasks and child workflows

You can create tasks and child flows to organize your workflow logic.
Each task and child flow is a separate unit of work and is displayed in the Prefect UI.

Cancel a workflow if it runs for too long

To apply a timeout to a flow or task to prevent it from running for too long, use the timeout_seconds keyword argument.

Task timeout behavior

Task timeouts work differently depending on how the task is executed: Async tasks: Timeouts use cooperative cancellation at await points. When the timeout is reached, the task is cancelled at the next await statement. This provides reliable timeout behavior for async code. Sync tasks called directly: When a sync task is called directly (not via .submit()), it typically runs on the main thread where timeouts can use OS signals to interrupt execution, including blocking operations. Sync tasks via .submit() with ThreadPoolTaskRunner: When a sync task is submitted using to a ThreadPoolTaskRunner (the default), it runs in a worker thread. In this context, timeouts cannot interrupt blocking operations like time.sleep(), network requests, or file I/O. The timeout will only take effect after the blocking operation completes naturally.
If you need reliable timeout behavior for sync tasks that perform blocking operations, consider:
  1. Using ProcessPoolTaskRunner instead of the default ThreadPoolTaskRunner
  2. Converting the task to async and using await-based I/O
  3. Breaking long blocking operations into smaller chunks to allow for interruption for cancellation
  4. Using libraries that support timeout parameters natively (e.g., requests.get(url, timeout=5))

Configure flows and tasks

Flow configuration

All flows can be configured by passing arguments to the decorator. Flows accept the following optional settings:

Task configuration

Tasks allow for customization through optional arguments that can be provided to the task decorator. See all possible options in the Python SDK docs. For example, provide optional name and description arguments to a task:
Distinguish runs of this task by providing a task_run_name.
Python’s standard string formatting syntax applies:
Additionally, this setting accepts a function that returns a string for the task run name: