Documentation Index
Fetch the complete documentation index at: https://docs.pyworkflow.dev/llms.txt
Use this file to discover all available pages before exploring further.
Overview
PyWorkflow supports graceful workflow cancellation. When you cancel a workflow, it will terminate at the next checkpoint rather than being forcefully killed, allowing for proper cleanup.Graceful Termination
Workflows stop at safe checkpoints, not mid-operation.
Cleanup Support
Catch
CancellationError to perform cleanup before terminating.Shield Critical Code
Use
shield() to protect code that must complete.CLI & API
Cancel via CLI command or programmatic API.
Cancelling a Workflow
- Python API
- CLI
Use
cancel_workflow() to request cancellation:How Cancellation Works
Cancellation in PyWorkflow is checkpoint-based. The workflow is cancelled at the next checkpoint, not immediately.Cancellation Checkpoints
Cancellation is checked at these points:| Checkpoint | When |
|---|---|
| Before each step | Before @step decorated functions execute |
| Before sleep | Before await sleep() suspends the workflow |
| Before hook | Before await hook() suspends the workflow |
Cooperative Cancellation for Long-Running Steps
For steps that run for a long time, you can add cooperative cancellation checks:check_cancellation() method is async because in durable mode it queries the storage backend’s cancellation flag, enabling detection of external cancellation requests (e.g., from cancel_workflow()). It checks the in-memory flag first as a fast path, then falls back to storage if needed.
This allows the step to respond to cancellation requests between chunks rather than waiting until the entire dataset is processed.
Handling Cancellation
Workflows can catchCancellationError to perform cleanup before terminating:
The shield() Context Manager
Use shield() to protect critical code from cancellation:
shield() block:
await ctx.check_cancellation()will not raiseCancellationError- The cancellation request is preserved
- Cancellation will take effect after exiting the shield
Workflow States
When a workflow is cancelled, its status transitions toCANCELLED:
| Workflow State | Cancellation Behavior |
|---|---|
RUNNING | Sets cancellation flag; workflow cancelled at next checkpoint |
SUSPENDED | Immediately marked as CANCELLED; scheduled resume task is abandoned |
COMPLETED | Cannot be cancelled (returns False) |
FAILED | Cannot be cancelled (returns False) |
CANCELLED | Already cancelled (returns False) |
Monitoring Cancelled Workflows
Use the CLI to view cancelled workflows:Best Practices
Always handle CancellationError for cleanup
Always handle CancellationError for cleanup
If your workflow allocates resources or makes changes that need to be reversed, catch
CancellationError and clean up:Use shield() sparingly
Use shield() sparingly
Only use
shield() for truly critical operations like database commits or compensation logic. Long-running shielded operations delay cancellation.Add cooperative checks in long steps
Add cooperative checks in long steps
For steps that process large amounts of data, add periodic cancellation checks:
Provide cancellation reasons
Provide cancellation reasons
Include a reason when cancelling for better debugging and audit trails:
API Reference
cancel_workflow()
| Parameter | Type | Default | Description |
|---|---|---|---|
run_id | str | required | Workflow run ID to cancel |
reason | str | None | Optional reason for cancellation |
wait | bool | False | Wait for workflow to reach terminal state |
timeout | float | None | Timeout in seconds when waiting |
storage | StorageBackend | None | Storage backend (uses configured default) |
True if cancellation was initiated, False if workflow is already in a terminal state.
CancellationError
shield()
Next Steps
Fault Tolerance
Learn about automatic recovery from worker crashes.
Hooks
Wait for external events in your workflows.
CLI Guide
Manage workflows from the command line.
Sleep
Pause workflows with durable sleep.