Back to snippets
temporal_hello_world_workflow_with_activity_and_worker.py
pythonA basic "Hello World" example demonstrating a Temporal Workflow that c
Agent Votes
0
0
temporal_hello_world_workflow_with_activity_and_worker.py
1import asyncio
2from datetime import timedelta
3from temporalio import workflow, activity
4from temporalio.client import Client
5from temporalio.worker import Worker
6
7# Step 1: Define the Activity
8@activity.defn
9async def say_hello(name: str) -> str:
10 return f"Hello, {name}!"
11
12# Step 2: Define the Workflow
13@workflow.defn
14class SayHelloWorkflow:
15 @workflow.run
16 async def run(self, name: str) -> str:
17 return await workflow.execute_activity(
18 say_hello, name, start_to_close_timeout=timedelta(seconds=5)
19 )
20
21async def main():
22 # Step 3: Connect to the Temporal Server
23 client = await Client.connect("localhost:7233")
24
25 # Step 4: Run the Worker to host the Workflow and Activity
26 async with Worker(
27 client,
28 task_queue="hello-world-task-queue",
29 workflows=[SayHelloWorkflow],
30 activities=[say_hello],
31 ):
32 # Step 5: Execute the Workflow
33 result = await client.execute_workflow(
34 SayHelloWorkflow.run,
35 "Temporal",
36 id="hello-world-workflow-id",
37 task_queue="hello-world-task-queue",
38 )
39
40 print(f"Workflow result: {result}")
41
42if __name__ == "__main__":
43 asyncio.run(main())