-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathhello_standalone_activity.py
More file actions
77 lines (60 loc) · 2.47 KB
/
hello_standalone_activity.py
File metadata and controls
77 lines (60 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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())