In the Temporal Python SDK, you define Workflow Definitions by creating a class and annotate it with the @workflow.defn
decorator.
You then use the @workflow.run
decorator to specify the method that the Workflow should invoke. Exactly one method must have this decorator and it must be added to an async def
method.
from datetime import timedelta
from temporalio import workflow
# Import activity, passing it through the sandbox without reloading the module
with workflow.unsafe.imports_passed_through():
from activities import say_hello
@workflow.defn
class SayHello:
@workflow.run
async def run(self, name: str) -> str:
return await workflow.execute_activity(
say_hello, name, start_to_close_timeout=timedelta(seconds=5)
)
In a Temporal Application, Activities are where you execute non-deterministic code or perform operations that may fail, such as API requests or database calls. Your Workflow Definitions call Activities and process the results. Complex Temporal Applications have Workflows that invoke many Activities, using the results of one Activity to execute another.
In the Temporal Python SDK, you define an Activity by decorating a function with @activity.defn
.
Create a new file called activities.py
and add the following code to define a say_hello
function to define the Activity:
from temporalio import activity
@activity.defn
asyncdefsay_hello(name:str)->str:
returnf"Hello, {name}!"
A Worker hosts Workflow and Activity functions and executes them. The Temporal Cluster tells the Worker to execute a specific function from information it pulls from the Task Queue. After the Worker runs the code, it communicates the results back to the Temporal Cluster.
When you start a Workflow, you tell the server which Task Queue the Workflow and Activities use. A Worker listens and polls on the Task Queue, looking for work to do.
To configure a Worker process using the Python SDK, you’ll connect to the Temporal Cluster and give it the name of the Task Queue to poll.
You’ll connect to the Temporal Cluster using a Temporal Client, which provides a set of APIs to communicate with a Temporal Cluster. You’ll use Clients to interact with existing Workflows or to start new ones.