Schedules - Python SDK
This page shows how to do the following:
Schedule a Workflow
How to Schedule a Workflow Execution
Scheduling Workflows is a crucial aspect of any automation process, especially when dealing with time-sensitive tasks. By scheduling a Workflow, you can automate repetitive tasks, reduce the need for manual intervention, and ensure timely execution of your business processes
Use any of the following action to help Schedule a Workflow Execution and take control over your automation process.
Create a Scheduled Workflow
How to create a Scheduled Workflow
The create action enables you to create a new Schedule. When you create a new Schedule, a unique Schedule ID is generated, which you can use to reference the Schedule in other Schedule commands.
To create a Scheduled Workflow Execution in Python, use the create_schedule()
asynchronous method on the Client.
Then pass the Schedule ID and the Schedule object to the method to create a Scheduled Workflow Execution.
Set the action
parameter to ScheduleActionStartWorkflow
to start a Workflow Execution.
Optionally, you can set the spec
parameter to ScheduleSpec
to specify the schedule or set the intervals
parameter to ScheduleIntervalSpec
to specify the interval.
Other options include: cron_expressions
, skip
, start_at
, and jitter
.
View the source code
in the context of the rest of the application code.
# ...
async def main():
client = await Client.connect("localhost:7233")
await client.create_schedule(
"workflow-schedule-id",
Schedule(
action=ScheduleActionStartWorkflow(
YourSchedulesWorkflow.run,
"my schedule arg",
id="schedules-workflow-id",
task_queue="schedules-task-queue",
),
spec=ScheduleSpec(
intervals=[ScheduleIntervalSpec(every=timedelta(minutes=2))]
),
state=ScheduleState(note="Here's a note on my Schedule."),
),
)
Once a Schedule has completed creating all its Workflow Executions, the Temporal Service deletes it since it won’t fire again. The Temporal Service doesn't guarantee when this removal will happen.
Backfill a Scheduled Workflow
How to backfill a Scheduled Workflow
The backfill action executes Actions ahead of their specified time range. This command is useful when you need to execute a missed or delayed Action, or when you want to test the Workflow before its scheduled time.
To Backfill a Scheduled Workflow Execution in Python, use the backfill() asynchronous method on the Schedule Handle.
View the source code
in the context of the rest of the application code.
import asyncio
from datetime import datetime, timedelta
from temporalio.client import Client, ScheduleBackfill, ScheduleOverlapPolicy
async def main():
client = await Client.connect("localhost:7233")
handle = client.get_schedule_handle(
"workflow-schedule-id",
)
now = datetime.utcnow()
(
await handle.backfill(
ScheduleBackfill(
start_at=now - timedelta(minutes=10),
end_at=now - timedelta(minutes=9),
overlap=ScheduleOverlapPolicy.ALLOW_ALL,
),
),
)
Delete a Scheduled Workflow
How to delete a Scheduled Workflow
The delete action enables you to delete a Schedule. When you delete a Schedule, it does not affect any Workflows that were started by the Schedule.
To delete a Scheduled Workflow Execution in Python, use the delete() asynchronous method on the Schedule Handle.
View the source code
in the context of the rest of the application code.
async def main():
client = await Client.connect("localhost:7233")
handle = client.get_schedule_handle(
"workflow-schedule-id",
)
await handle.delete()
Describe a Scheduled Workflow
How to describe a Scheduled Workflow
The describe action shows the current Schedule configuration, including information about past, current, and future Workflow Runs. This command is helpful when you want to get a detailed view of the Schedule and its associated Workflow Runs.
To describe a Scheduled Workflow Execution in Python, use the describe() asynchronous method on the Schedule Handle. You can get a complete list of the attributes of the Scheduled Workflow Execution from the ScheduleDescription class.
View the source code
in the context of the rest of the application code.
# ...
async def main():
client = await Client.connect("localhost:7233")
handle = client.get_schedule_handle(
"workflow-schedule-id",
)
desc = await handle.describe()
print(f"Returns the note: {desc.schedule.state.note}")