What is Sleep?
Thesleep() 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
String Format (Recommended)
Timedelta
Until Specific Time
Integer (Seconds)
Zero-Resource Suspension
Traditional async sleep blocks a 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:
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:Docker Compose Setup
Best Practices
Use sleep for intentional delays only
Use sleep for intentional delays only
Don’t use sleep as a retry mechanism. Use step retry configuration instead:
Consider timezone implications
Consider timezone implications
When sleeping until a specific time, be aware of timezones:
Keep sleep durations reasonable
Keep sleep durations reasonable
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.