Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
<!-- Keep this list in alphabetical order -->
* [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.
Expand Down
3 changes: 2 additions & 1 deletion hello/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
77 changes: 77 additions & 0 deletions hello/hello_standalone_activity.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,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())
Loading