mirror of
https://github.com/php-win-ext/grpc.git
synced 2026-03-24 17:12:19 +01:00
- Switched from yapf to black - Reconfigure isort for black - Resolve black/pylint idiosyncrasies Note: I used `--experimental-string-processing` because black was producing "implicit string concatenation", similar to what described here: https://github.com/psf/black/issues/1837. While currently this feature is experimental, it will be enabled by default: https://github.com/psf/black/issues/2188. After running black with the new string processing so that the generated code merges these `"hello" " world"` strings concatenations, then I removed `--experimental-string-processing` for stability, and regenerated the code again. To the reviewer: don't even try to open "Files Changed" tab 😄 It's better to review commit-by-commit, and ignore `run black and isort`.
gRPC Python Async Interceptor Example
This example demonstrate the usage of Async interceptors and context propagation using contextvars.
When to use contextvars
Contextvars can be used to propagate context in a same thread or coroutine, some example usage include:
- Propagate from interceptor to another interceptor.
- Propagate from interceptor to the server handler.
- Propagate from client to server.
How does this example works
This example have the following steps:
- Generate RPC ID on client side and propagate to server using
metadata.contextvarscan be used here if client and server is running in a same coroutine (or same thead for Sync).
- Server interceptor1 intercept the request, it checks
rpc_id_varand decorate it with it's tagInterceptor1. - Server interceptor2 intercept the request, it checks
rpc_id_varand decorate it with it's tagInterceptor2. - Server handler receives the request with
rpc_id_vardecorated by both interceptor1 and interceptor2.
How to run this example
- Start server:
python3 -m async_greeter_server_with_interceptor - Start client:
python3 -m async_greeter_client
Expected outcome
- On client side, you should see logs similar to:
Sending request with rpc id: 59ac966558b3d7d11a06bd45f1a0f89d
Greeter client received: Hello, you!
- On server side, you should see logs similar to:
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.