MM
08/03/2025, 3:44 PMMarvin
08/03/2025, 3:45 PMMarvin
08/03/2025, 3:46 PMpython
from prefect import flow
from prefect.schedules import CronSchedule, IntervalSchedule, RRuleSchedule
from datetime import timedelta
from dateutil.rrule import rrule, DAILY
@flow
def my_flow():
print("Flow is running!")
# Method 1: Using Cron Schedule (e.g., run at 9 AM every day)
deployment = my_flow.to_deployment(
name="cron-scheduled-deployment",
schedule=CronSchedule(cron="0 9 * * *")
)
# Method 2: Using Interval Schedule (e.g., run every 2 hours)
deployment = my_flow.to_deployment(
name="interval-scheduled-deployment",
schedule=IntervalSchedule(interval=timedelta(hours=2))
)
# Method 3: Using RRule Schedule (e.g., run every weekday at 5 PM)
deployment = my_flow.to_deployment(
name="rrule-scheduled-deployment",
schedule=RRuleSchedule(
rrule=rrule(freq=DAILY, byhour=17, byminute=0)
)
)
# Deploy the flow with the schedule
deployment.deploy()
Here's what each schedule type is best for:
1. CronSchedule: Best for traditional cron-style scheduling. Great if you're familiar with cron syntax.
python
# Examples:
CronSchedule(cron="0 9 * * *") # Run at 9 AM daily
CronSchedule(cron="*/15 * * * *") # Run every 15 minutes
CronSchedule(cron="0 9 * * 1-5") # Run at 9 AM on weekdays
2. IntervalSchedule: Best for simple, regular intervals.
python
# Examples:
IntervalSchedule(interval=timedelta(hours=2)) # Every 2 hours
IntervalSchedule(interval=timedelta(days=1)) # Every day
3. RRuleSchedule: Best for complex calendar-based schedules.
python
# Examples:
from dateutil.rrule import rrule, DAILY, WEEKLY
# Every Monday and Wednesday at 3 PM
RRuleSchedule(
rrule=rrule(freq=WEEKLY, byweekday=[0,2], byhour=15, byminute=0)
)
Additional features for all schedule types:
- You can set a timezone: CronSchedule(cron="0 9 * * *", timezone="America/New_York")
- You can make schedules inactive: CronSchedule(cron="0 9 * * *", active=False)
Remember that after creating the deployment with a schedule, you'll need:
1. A work pool and worker running to execute the scheduled flows
2. To deploy the flow using .deploy()
The schedule will start working once these conditions are met.
Links for further reading:
- Prefect Schedules Concepts
- How to Create Schedules
Is there anything specific about scheduling that you'd like me to clarify?