|
| 1 | +import asyncio |
| 2 | +import logging |
| 3 | +from datetime import timedelta |
| 4 | + |
| 5 | +from temporalio import activity, workflow |
| 6 | +from temporalio.client import Client |
| 7 | +from temporalio.worker import Worker |
| 8 | + |
| 9 | + |
| 10 | +# Basic activity that logs and does string concatenation |
| 11 | +@activity.defn |
| 12 | +async def say_hello_activity(name: str) -> str: |
| 13 | + activity.logger.info("Running activity with parameter %s" % name) |
| 14 | + return f"Hello, {name}!" |
| 15 | + |
| 16 | + |
| 17 | +# Basic workflow that logs and invokes an activity |
| 18 | +@workflow.defn |
| 19 | +class SayHelloWorkflow: |
| 20 | + @workflow.run |
| 21 | + async def run(self, name: str) -> str: |
| 22 | + workflow.logger.info("Running workflow with parameter %s" % name) |
| 23 | + return await workflow.execute_activity( |
| 24 | + say_hello_activity, name, start_to_close_timeout=timedelta(seconds=10) |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +async def main(): |
| 29 | + # Uncomment the line below to see logging |
| 30 | + # logging.basicConfig(level=logging.INFO) |
| 31 | + |
| 32 | + # Start client |
| 33 | + client = await Client.connect("http://localhost:7233") |
| 34 | + |
| 35 | + # Run a worker for the workflow |
| 36 | + async with Worker( |
| 37 | + client, |
| 38 | + task_queue="my-task-queue", |
| 39 | + workflows=[SayHelloWorkflow], |
| 40 | + activities=[say_hello_activity], |
| 41 | + ): |
| 42 | + |
| 43 | + # While the worker is running, use the client to run the workflow and |
| 44 | + # print out its result. Note, in many production setups, the client |
| 45 | + # would be in a completely separate process from the worker. |
| 46 | + result = await client.execute_workflow( |
| 47 | + SayHelloWorkflow.run, |
| 48 | + "Temporal", |
| 49 | + id="my-workflow-id", |
| 50 | + task_queue="my-task-queue", |
| 51 | + ) |
| 52 | + print(f"Result: {result}") |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == "__main__": |
| 56 | + asyncio.run(main()) |
0 commit comments