Skip to content

Commit 400c5a9

Browse files
committed
Add documentation of interceptors to README
1 parent f4ca16d commit 400c5a9

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

README.md

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ informal introduction to the features and their implementation.
9494
- [Heartbeating and Cancellation](#heartbeating-and-cancellation)
9595
- [Worker Shutdown](#worker-shutdown)
9696
- [Testing](#testing-1)
97+
- [Interceptors](#interceptors)
9798
- [Nexus](#nexus)
9899
- [Workflow Replay](#workflow-replay)
99100
- [Observability](#observability)
@@ -1310,6 +1311,64 @@ affect calls activity code might make to functions on the `temporalio.activity`
13101311
* `worker_shutdown()` can be invoked to simulate a worker shutdown during execution of the activity
13111312

13121313

1314+
### Interceptors
1315+
1316+
The behavior of the SDK can be customized in many useful ways by modifying inbound and outbound calls using
1317+
interceptors. This is similar to the use of middleware in other frameworks.
1318+
1319+
There are five categories of inbound and outbound calls that you can modify in this way:
1320+
1321+
1. Outbound client calls, such as `start_workflow()`, `signal_workflow()`, `list_workflows()`, `update_schedule()`, etc.
1322+
1323+
2. Inbound workflow calls: `execute_workflow()`, `handle_signal()`, `handle_update_handler()`, etc
1324+
1325+
3. Outbound workflow calls: `start_activity()`, `start_child_workflow()`, `start_nexus_operation()`, etc
1326+
1327+
4. Inbound call to execute an activity: `execute_activity()`
1328+
1329+
5. Outbound activity calls: `info()` and `hearbeat()`
1330+
1331+
1332+
To modify outbound client calls, define a class inheriting from
1333+
[`client.Interceptor`](https://python.temporal.io/temporalio.client.Interceptor.html), and implement the method
1334+
`intercept_client()` to return an instance of
1335+
[`OutboundInterceptor`](https://python.temporal.io/temporalio.client.OutboundInterceptor.html) that implements the
1336+
subset of outbound client calls that you wish to modify.
1337+
1338+
Then, pass a list containing an instance of your `client.Interceptor` class as the
1339+
`interceptors` argument of [`Client.connect()`](https://python.temporal.io/temporalio.client.Client.html#connect).
1340+
1341+
1342+
The remaining four categories are worker calls. To modify these, define a class inheriting from
1343+
[`worker.Interceptor`](https://python.temporal.io/temporalio.worker.Interceptor.html) and implement methods on that
1344+
class to define the
1345+
[`ActivityInboundInterceptor`](https://python.temporal.io/temporalio.worker.ActivityInboundInterceptor.html),
1346+
[`ActivityOutboundInterceptor`](https://python.temporal.io/temporalio.worker.ActivityOutboundInterceptor.html),
1347+
[`WorkflowInboundInterceptor`](https://python.temporal.io/temporalio.worker.WorkflowInboundInterceptor.html), and
1348+
[`WorkflowOutboundInterceptor`](https://python.temporal.io/temporalio.worker.WorkflowOutboundInterceptor.html) classes
1349+
that you wish to use to effect your modifications.
1350+
1351+
Then, pass a list containing an instance of your `worker.Interceptor` class as the `interceptors` argument of
1352+
`Client.connect()`.
1353+
1354+
You can also pass worker interceptors as the `interceptor` argument to the
1355+
[`Worker()`](https://python.temporal.io/temporalio.worker.Worker.html) constructor but, if you do, do not pass the same
1356+
ones to `Client.connect()`. Finally, for convenience, it's common to define a class inheriting from _both_
1357+
`client.Interceptor` and `worker.Interceptor` (their method sets do not overlap), and define all your interceptor
1358+
customizations in the methods of that class.
1359+
1360+
This is best explained by example. The [Context Propagation Interceptor
1361+
Sample](https://github.com/temporalio/samples-python/tree/main/context_propagation) is a good starting point. In
1362+
[context_propagation/interceptor.py](https://github.com/temporalio/samples-python/blob/main/context_propagation/interceptor.py)
1363+
a class is defined that inherits from both `client.Interceptor` and `worker.Interceptor`. It implements the various
1364+
methods such that the outbound client and workflow calls set a certain key in the outbound `headers` field, and the
1365+
inbound workflow and activity calls retrieve the header value from the inbound workflow/activity input data. An instance
1366+
of this interceptor class is passed to `Client.connect` when [starting the
1367+
worker](https://github.com/temporalio/samples-python/blob/main/context_propagation/worker.py) and when connecting the
1368+
client in the [workflow starter
1369+
code](https://github.com/temporalio/samples-python/blob/main/context_propagation/starter.py).
1370+
1371+
13131372
### Nexus
13141373

13151374
⚠️ **Nexus support is currently at an experimental release stage. Backwards-incompatible changes are anticipated until a stable release is announced.** ⚠️

0 commit comments

Comments
 (0)