diff --git a/README.md b/README.md index 5a39e834..8e7748de 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,8 @@ Some examples require extra dependencies. See each sample's directory for specif * [hello_search_attributes](hello/hello_search_attributes.py) - Start workflow with search attributes then change while running. * [hello_signal](hello/hello_signal.py) - Send signals to a workflow. + * [hello standalone activity](hello/hello_standalone_activity.py) - Execute an activity from outside of a workflow. + * [hello update](hello/hello_update.py) - Send a request to and a response from a client to a workflow execution. * [activity_worker](activity_worker) - Use Python activities from a workflow in another language. * [batch_sliding_window](batch_sliding_window) - Batch processing with a sliding window of child workflows. diff --git a/hello/README.md b/hello/README.md index c014d08c..48e8e67e 100644 --- a/hello/README.md +++ b/hello/README.md @@ -44,7 +44,8 @@ Replace `hello/hello_activity.py` in the command with any other example filename * [hello_search_attributes](hello_search_attributes.py) - Start workflow with search attributes then change while running. * [hello_signal](hello_signal.py) - Send signals to a workflow. -* [hello_update](hello_update.py) - Send a request to and a response from a client to a workflow execution. +* [hello standalone activity](hello_standalone_activity.py) - Execute an activity from outside of a workflow. +* [hello_update](hello_update.py) - **Send a request to and a response from a client to a workflow execution.** Note: To enable the workflow update, set the `frontend.enableUpdateWorkflowExecution` dynamic config value to true. diff --git a/hello/hello_standalone_activity.py b/hello/hello_standalone_activity.py new file mode 100644 index 00000000..c53d9da9 --- /dev/null +++ b/hello/hello_standalone_activity.py @@ -0,0 +1,75 @@ +import asyncio +from dataclasses import dataclass +from datetime import timedelta + +from temporalio import activity +from temporalio.client import Client +from temporalio.envconfig import ClientConfig +from temporalio.worker import Worker + +# This sample is very similar to hello_activity.py. The difference is that whereas in +# hello_activity.py the activity is orchestrated by a workflow, in this sample the activity is +# executed directly by a client ("standalone activity"). + + +@dataclass +class ComposeGreetingInput: + greeting: str + name: str + + +# This is just a normal activity. You could invoke it from a workflow but, in this sample, we are +# invoking it directly as a standalone activity. +@activity.defn +async def compose_greeting(input: ComposeGreetingInput) -> str: + activity.logger.info("Running activity with parameter %s" % input) + return f"{input.greeting}, {input.name}!" + + +async def my_client_code(client: Client): + # client.execute_activity starts the activity, and then uses a long-poll to wait for the + # activity to be completed by the worker. + result = await client.execute_activity( + compose_greeting, + args=[ComposeGreetingInput("Hello", "World")], + id="my-standalone-activity-id", + task_queue="hello-standalone-activity-task-queue", + start_to_close_timeout=timedelta(seconds=10), + ) + print(f"Activity result: {result}") + + activities = client.list_activities( + query="TaskQueue = 'hello-standalone-activity-task-queue'" + ) + print("ListActivity results:") + async for info in activities: + print(f"\tActivityID: {info.activity_id}, Type: {info.activity_type}, Status: {info.status}") + + count_result = await client.count_activities( + query="TaskQueue = 'hello-standalone-activity-task-queue'" + ) + print(f"Total activities: {count_result.count}") + + +async def main(): + # Uncomment the lines below to see logging output + # import logging + # logging.basicConfig(level=logging.INFO) + + config = ClientConfig.load_client_connect_config() + config.setdefault("target_host", "localhost:7233") + + client = await Client.connect(**config) + + # Run a worker for the activity + async with Worker( + client, + task_queue="hello-standalone-activity-task-queue", + activities=[compose_greeting], + ): + # While the worker is running, use the client to execute the activity. + await my_client_code(client) + + +if __name__ == "__main__": + asyncio.run(main())