|
| 1 | +import asyncio |
| 2 | +from dataclasses import dataclass |
| 3 | +from datetime import timedelta |
| 4 | + |
| 5 | +from temporalio import activity |
| 6 | +from temporalio.client import Client |
| 7 | +from temporalio.envconfig import ClientConfig |
| 8 | +from temporalio.worker import Worker |
| 9 | + |
| 10 | +# This sample is very similar to hello_activity.py. The difference is that whereas in |
| 11 | +# hello_activity.py the activity is orchestrated by a workflow, in this sample the activity is |
| 12 | +# executed directly by a client ("standalone activity"). |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class ComposeGreetingInput: |
| 17 | + greeting: str |
| 18 | + name: str |
| 19 | + |
| 20 | + |
| 21 | +# This is just a normal activity. You could invoke it from a workflow but, in this sample, we are |
| 22 | +# invoking it directly as a standalone activity. |
| 23 | +@activity.defn |
| 24 | +async def compose_greeting(input: ComposeGreetingInput) -> str: |
| 25 | + activity.logger.info("Running activity with parameter %s" % input) |
| 26 | + return f"{input.greeting}, {input.name}!" |
| 27 | + |
| 28 | + |
| 29 | +async def my_client_code(client: Client): |
| 30 | + # client.execute_activity starts the activity, and then uses a long-poll to wait for the |
| 31 | + # activity to be completed by the worker. |
| 32 | + result = await client.execute_activity( |
| 33 | + compose_greeting, |
| 34 | + args=[ComposeGreetingInput("Hello", "World")], |
| 35 | + id="my-standalone-activity-id", |
| 36 | + task_queue="hello-standalone-activity-task-queue", |
| 37 | + start_to_close_timeout=timedelta(seconds=10), |
| 38 | + ) |
| 39 | + print(f"Activity result: {result}") |
| 40 | + |
| 41 | + activities = client.list_activities( |
| 42 | + query="TaskQueue = 'hello-standalone-activity-task-queue'" |
| 43 | + ) |
| 44 | + print("ListActivity results:") |
| 45 | + async for info in activities: |
| 46 | + print( |
| 47 | + f"\tActivityID: {info.activity_id}, Type: {info.activity_type}, Status: {info.status}" |
| 48 | + ) |
| 49 | + |
| 50 | + count_result = await client.count_activities( |
| 51 | + query="TaskQueue = 'hello-standalone-activity-task-queue'" |
| 52 | + ) |
| 53 | + print(f"Total activities: {count_result.count}") |
| 54 | + |
| 55 | + |
| 56 | +async def main(): |
| 57 | + # Uncomment the lines below to see logging output |
| 58 | + # import logging |
| 59 | + # logging.basicConfig(level=logging.INFO) |
| 60 | + |
| 61 | + config = ClientConfig.load_client_connect_config() |
| 62 | + config.setdefault("target_host", "localhost:7233") |
| 63 | + |
| 64 | + client = await Client.connect(**config) |
| 65 | + |
| 66 | + # Run a worker for the activity |
| 67 | + async with Worker( |
| 68 | + client, |
| 69 | + task_queue="hello-standalone-activity-task-queue", |
| 70 | + activities=[compose_greeting], |
| 71 | + ): |
| 72 | + # While the worker is running, use the client to execute the activity. |
| 73 | + await my_client_code(client) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == "__main__": |
| 77 | + asyncio.run(main()) |
0 commit comments