Skip to main content

What is Sleep?

The sleep() primitive pauses a workflow for a specified duration. Unlike traditional sleep that blocks a thread, PyWorkflow’s sleep suspends the workflow completely - no resources are consumed during the sleep period.

How It Works

Duration Formats

Timedelta

Until Specific Time

Integer (Seconds)

Zero-Resource Suspension

Traditional async sleep blocks a worker:
PyWorkflow’s sleep releases the worker:
With 100 workflows each sleeping for 1 day, traditional sleep would need 100 workers blocked for 24 hours. PyWorkflow needs 0 workers during the sleep period.

Sleep within Steps

sleep() can also be called from within @step functions. However, when called from a step, it uses asyncio.sleep instead of durable suspension, since steps cannot suspend and resume:
Sleep within steps is not durable: the worker is held during the sleep, and if the worker crashes, the sleep state is lost. For long delays, use workflow-level sleep instead.

Use Cases

Scheduled Reminders

Delayed Processing

Rate Limiting

Retry with Backoff

Sleep vs Step Timeout

Sleep and timeouts serve different purposes:

Celery Beat Requirement

Sleep resumption requires Celery Beat to be running:
Without Celery Beat, workflows will suspend but never resume automatically. Make sure Beat is running in production.

Docker Compose Setup

Best Practices

Don’t use sleep as a retry mechanism. Use step retry configuration instead:
When sleeping until a specific time, be aware of timezones:
Very long sleeps (months, years) work but consider if a different approach is better:

Next Steps

Workflows

Learn about workflow orchestration.

Deployment

Set up Celery Beat in production.