Skip to content

Commit 57363e8

Browse files
committed
Improve comments
1 parent 610b417 commit 57363e8

File tree

4 files changed

+51
-15
lines changed

4 files changed

+51
-15
lines changed

hello_nexus/basic/caller/workflows.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,30 @@
66
NEXUS_ENDPOINT = "my-nexus-endpoint"
77

88

9+
# This is a workflow that calls a nexus operation.
910
@workflow.defn
1011
class CallerWorkflow:
12+
# An __init__ method is always optional on a Workflow class. Here we use it to set the
13+
# NexusClient, but that could alternatively be done in the run method.
1114
def __init__(self):
1215
self.nexus_client = NexusClient(
1316
MyNexusService,
1417
endpoint=NEXUS_ENDPOINT,
1518
)
1619

20+
# The Wokflow run method invokes two Nexus operations.
1721
@workflow.run
1822
async def run(self, name: str) -> tuple[MyOutput, MyOutput]:
19-
sync_result: MyOutput = await self.nexus_client.execute_operation(
20-
MyNexusService.my_sync_operation,
21-
MyInput(name),
22-
)
23+
# Start the Nexus operation and wait for the result in one go, using execute_operation.
2324
wf_result: MyOutput = await self.nexus_client.execute_operation(
2425
MyNexusService.my_workflow_run_operation,
2526
MyInput(name),
2627
)
28+
# We could use execute_operation for this one also, but here we demonstrate
29+
# obtaining the operation handle and then using it to get the result.
30+
sync_operation_handle = await self.nexus_client.start_operation(
31+
MyNexusService.my_sync_operation,
32+
MyInput(name),
33+
)
34+
sync_result = await sync_operation_handle
2735
return sync_result, wf_result

hello_nexus/basic/handler/service_handler.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
"""
2-
This file demonstrates how to define operation handlers by implementing the `start`
3-
method only, using the "shorthand" decorators sync_operation_handler and
4-
workflow_run_operation_handler.
2+
This file demonstrates how to define operation handlers by using the "shorthand"
3+
decorators sync_operation_handler and workflow_run_operation_handler. In this style you
4+
implement the `start` method only. workflow_run_operation_handler implements `cancel` for
5+
you automatically, but apart from that, the other operation methods (`fetch_info`,
6+
`fetch_result`, and `cancel` for sync_operation_handler) are all automatically created
7+
with "raise NotImplementedError" implementations.
8+
9+
See hello_nexus/basic/handler/service_handler_with_operation_handler_classes.py for the
10+
alternative "fully manual" style where you implement an OperationHandler class directly.
511
"""
612

713
from __future__ import annotations

hello_nexus/basic/handler/service_handler_with_operation_handler_classes.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
"""
2-
Notes:
2+
This file demonstrates how to define operation handlers by implementing an OperationHandler
3+
class directly.
4+
5+
See hello_nexus/basic/handler/service_handler.py for the alternative "shorthand" style
6+
where you implement the `start` method only.
37
48
Sync operations:
59
---------------
610
Implementations are free to make arbitrary network calls, or perform CPU-bound
7-
computations such as this one. Total execution duration must not exceed 10s. To
8-
perform Temporal client calls such as signaling/querying/listing workflows, use
9-
self.client.
11+
computations such as this one. Total execution duration must not exceed 10s.
1012
1113
1214
Workflow operations:
@@ -62,11 +64,16 @@ def my_workflow_run_operation(
6264
return MyWorkflowRunOperation()
6365

6466

67+
# This is a Nexus operation that responds synchronously to all requests.
6568
class MySyncOperation(OperationHandler[MyInput, MyOutput]):
6669
# You can add an __init__ method taking any required arguments, since you are in
6770
# control of instantiating the OperationHandler inside the operation handler method
6871
# above decorated with @operation_handler.
6972

73+
# Unlike the workflow run operation below, the `start` method for a sync operation
74+
# returns the final operation result. Sync operations are free to make arbitrary
75+
# network calls, or perform CPU-bound computations. Total execution duration must not
76+
# exceed 10s. async def start(
7077
async def start(
7178
self, ctx: StartOperationContext, input: MyInput
7279
) -> StartOperationResultSync[MyOutput]:
@@ -103,7 +110,21 @@ async def cancel(
103110
)
104111

105112

113+
# This is a Nexus operation that is backed by a Temporal workflow. That means that it
114+
# responds asynchronously to all requests: it starts a workflow and responds with a token
115+
# that the handler can associate with the worklow is started.
106116
class MyWorkflowRunOperation(OperationHandler[MyInput, MyOutput]):
117+
# You can add an __init__ method taking any required arguments, since you are in
118+
# control of instantiating the OperationHandler inside the operation handler method
119+
# above decorated with @operation_handler.
120+
121+
# The start method starts a workflow, and returns a WorkflowRunOperationResult that it
122+
# creates from the workflow handle. This return value contains the Nexus operation
123+
# token that the handler can use to obtain a handle and interact with the workflow on
124+
# future requests (for example if a cancel request is subsequently sent by the
125+
# caller). The Temporal server takes care of delivering the workflow result to the
126+
# calling workflow. The task queue defaults to the task queue being used by the Nexus
127+
# worker.
107128
async def start(
108129
self, ctx: StartOperationContext, input: MyInput
109130
) -> WorkflowRunOperationResult:

hello_nexus/basic/handler/worker.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ async def main(
3232
)
3333

3434
# Create an instance of the service handler. Your service handler class __init__ can
35-
# take anything that your operation handlers need when handling requests. In this
36-
# example we provide a database client object to the service hander.
35+
# be written to accept any arguments that your operation handlers need when handling
36+
# requests. In this example we provide a database client object to the service hander.
3737
connected_db_client = MyDBClient.connect()
3838

3939
my_nexus_service_handler = (
@@ -45,8 +45,9 @@ async def main(
4545
)
4646

4747
# Start the worker, passing the Nexus service handler instance, in addition to the
48-
# workflow class that is used by the workflow-backed nexus operation. This Worker will
49-
# poll for both workflow tasks and Nexus tasks.
48+
# workflow classes that are started by your nexus operations, and any activities
49+
# needed. This Worker will poll for both workflow tasks and Nexus tasks (this example
50+
# doesn't use any activities).
5051
async with Worker(
5152
client,
5253
task_queue=TASK_QUEUE,

0 commit comments

Comments
 (0)