This example demonstrate the usage of Async interceptors and context propagation using contextvars.
Contextvars
can be used to propagate context in a same thread or coroutine, some example usage include:
This example have the following steps:
metadata
.contextvars
can be used here if client and server is running in a same coroutine (or same thead for Sync).rpc_id_var
and decorate it with its tag Interceptor1
.rpc_id_var
and decorate it with its tag Interceptor2
.rpc_id_var
decorated by both interceptor1 and interceptor2.python3 -m async_greeter_server_with_interceptor
python3 -m async_greeter_client
Sending request with rpc id: 59ac966558b3d7d11a06bd45f1a0f89d Greeter client received: Hello, you!
INFO:root:Starting server on [::]:50051 INFO:root:Interceptor1 called with rpc_id: default INFO:root:Interceptor2 called with rpc_id: Interceptor1-59ac966558b3d7d11a06bd45f1a0f89d INFO:root:Handle rpc with id Interceptor2-Interceptor1-59ac966558b3d7d11a06bd45f1a0f89d in server handler.