Skip to content

Commit e2381f7

Browse files
dandavisonyuandrew
andauthored
Standalone activity (temporalio#270)
* Standalone Activity sample * Add sample to READMEs, update sample to include list_activities and count_activities * ruff format --------- Co-authored-by: Andrew Yuan <Andrew.Yuan@temporal.io>
1 parent 827d565 commit e2381f7

3 files changed

Lines changed: 81 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ Some examples require extra dependencies. See each sample's directory for specif
5555
* [hello_search_attributes](hello/hello_search_attributes.py) - Start workflow with search attributes then change
5656
while running.
5757
* [hello_signal](hello/hello_signal.py) - Send signals to a workflow.
58+
* [hello standalone activity](hello/hello_standalone_activity.py) - Execute an activity from outside of a workflow.
59+
* [hello update](hello/hello_update.py) - Send a request to and a response from a client to a workflow execution.
5860
<!-- Keep this list in alphabetical order -->
5961
* [activity_worker](activity_worker) - Use Python activities from a workflow in another language.
6062
* [batch_sliding_window](batch_sliding_window) - Batch processing with a sliding window of child workflows.

hello/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ Replace `hello/hello_activity.py` in the command with any other example filename
4444
* [hello_search_attributes](hello_search_attributes.py) - Start workflow with search attributes then change while
4545
running.
4646
* [hello_signal](hello_signal.py) - Send signals to a workflow.
47-
* [hello_update](hello_update.py) - Send a request to and a response from a client to a workflow execution.
47+
* [hello standalone activity](hello_standalone_activity.py) - Execute an activity from outside of a workflow.
48+
* [hello_update](hello_update.py) - **Send a request to and a response from a client to a workflow execution.**
4849

4950
Note: To enable the workflow update, set the `frontend.enableUpdateWorkflowExecution` dynamic config value to true.
5051

hello/hello_standalone_activity.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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

Comments
 (0)