You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/develop/python/message-passing.mdx
+45-3Lines changed: 45 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -436,7 +436,7 @@ class GreetingWorkflow:
436
436
returnself.greetings[self.language]
437
437
```
438
438
439
-
### Use wait conditions in handlers
439
+
### Use wait conditions in handlers{#wait-in-message-handler}
440
440
441
441
It's common to use a Workflow wait condition to wait until a handler should start.
442
442
You can also use wait conditions anywhere else in the handler to wait for a specific condition to become `True`.
@@ -453,8 +453,6 @@ The `workflow.wait_condition` method waits until your condition is met:
453
453
)
454
454
```
455
455
456
-
Remember: handlers can execute before the main Workflow method starts.
457
-
458
456
You can also use wait conditions anywhere else in the handler to wait for a specific condition to become true.
459
457
This allows you to write handlers that pause at multiple points, each time waiting for a required condition to become true.
460
458
@@ -486,6 +484,50 @@ You can silence these warnings on a per-handler basis by passing the `unfinished
486
484
487
485
See [Finishing handlers before the Workflow completes](/encyclopedia/workflow-message-passing#finishing-message-handlers) for more information.
488
486
487
+
488
+
### Use `@workflow.init` to operate on Workflow input before any handler executes
489
+
490
+
Normally, your Workflow `__init__` method won't have any parameters.
491
+
However, if you use the `@workflow.init` decorator on your `__init__` method, you can give it the same [Workflow parameters](/develop/python/core-application#workflow-parameters) as your `@workflow.run` method.
492
+
The SDK will then ensure that your `__init__` method receives the Workflow input arguments that the [Client sent](/develop/python/coretemporal-clients#start-workflow-execution).
493
+
(The Workflow input arguments are also passed to your `@workflow.run` method -- that always happens, whether or not you use the `@workflow.init` decorator.)
494
+
495
+
To understand why `@workflow.init` is useful, suppose that you've written a Signal or Update handler that does something involving the Workflow input, but that you need to process the workflow input in some way before the handler should be allowed to access it.
496
+
The problem is that it's possible for a Signal or Update handler to start executing _before_ your `@workflow.run` method!
497
+
A solution is to use the `@workflow.init` decorator, and do the processing in your `__init__` method. (An alternative solution would be to [use `@workflow.wait_condition` in your handler](#wait-in-message-handler) to wait until the processing has been done by your `@workflow.run` method.
498
+
That would work, but using `@workflow.init` is simpler.)
499
+
500
+
Here's an example.
501
+
Notice that `__init__` and `get_greeting` have the same parameters, with the same type annotations:
0 commit comments