Quick Start
How It Works
The AsyncAgentScheduler uses async-native execution with cooperative cancellation, replacing the old thread-based scheduler.Schedule Expression Reference
| Expression | Interval | Description |
|---|---|---|
"daily" | 86400s | Every 24 hours |
"hourly" | 3600s | Every hour |
"*/30m" | 1800s | Every 30 minutes |
"*/1h" | 3600s | Every 1 hour |
"*/5s" | 5s | Every 5 seconds |
"60" | 60s | Custom seconds (plain digits) |
Configuration Options
AsyncAgentScheduler Constructor
| Parameter | Type | Default | Description |
|---|---|---|---|
agent | Any | Required | Agent instance to schedule |
task | str | Required | Task description to execute |
config | Optional[Dict[str, Any]] | None | Optional configuration dictionary |
on_success | Optional[Callable[[Any], None]] | None | Callback function on successful execution |
on_failure | Optional[Callable[[Exception], None]] | None | Callback function on failed execution |
start() Method Options
| Parameter | Type | Default | Description |
|---|---|---|---|
schedule_expr | str | Required | Schedule expression (e.g., “hourly”, ”*/1h”, “3600”) |
max_retries | int | 3 | Maximum retry attempts on failure |
run_immediately | bool | False | If True, run agent immediately before starting schedule |
get_stats() Response
| Field | Type | Description |
|---|---|---|
is_running | bool | Whether scheduler is currently running |
execution_count | int | Total number of execution attempts |
success_count | int | Number of successful executions |
failure_count | int | Number of failed executions |
agent_name | str | Name of the agent being scheduled |
task | str | Task description being executed |
Common Patterns
Running in FastAPI Application
Error Handling with Logging
Graceful Shutdown on SIGINT
Best Practices
Always await scheduler.stop() before exiting
Always await scheduler.stop() before exiting
The
stop() method waits up to 30 seconds for the current execution to complete before canceling. This prevents data corruption and ensures clean shutdown.Use run_immediately=True for testing
Use run_immediately=True for testing
Enable
run_immediately=True to verify your agent works correctly before waiting for the first scheduled interval.Keep callbacks lightweight
Keep callbacks lightweight
Success and failure callbacks are called synchronously. Heavy operations should be offloaded to avoid blocking the scheduler.
Prefer AsyncAgentScheduler over legacy thread-based scheduler
Prefer AsyncAgentScheduler over legacy thread-based scheduler
For new code, use
AsyncAgentScheduler instead of the legacy AgentScheduler. The async version provides better cancellation, no daemon threads, and fits naturally into async applications.Migration from Legacy Scheduler:
AsyncAgentScheduler replaces the thread-based AgentScheduler for new applications. The sync-looking public CLI (praisonai schedule ...) is unchanged and continues to work as before. Use AsyncAgentScheduler when embedding PraisonAI in async applications like FastAPI, or when you need cooperative cancellation.Related
Scheduler CLI
Command-line interface for scheduling agents
Background Tasks
Running agents as background processes

