Files
grpc/examples/python/async_streaming
Sergii Tkachenko de6ed9ba9f [Python] Migrate from yapf to black (#33138)
- 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`.
2023-06-09 15:08:55 -07:00
..
2020-12-21 14:38:39 -08:00
2020-12-21 14:38:39 -08:00

gRPC Python Non-Blocking Streaming RPC Client Example

The goal of this example is to demonstrate how to handle streaming responses without blocking the current thread. Effectively, this can be achieved by converting the gRPC Python streaming API into callback-based.

In this example, the RPC service Phone simulates the life cycle of virtual phone calls. It requires one thread to handle the phone-call session state changes, and another thread to process the audio stream. In this case, the normal blocking style API could not fulfill the need easily. Hence, we should asynchronously execute the streaming RPC.

Steps to run this example

Start the server in one session

python3 server.py

Start the client in another session

python3 client.py

Example Output

$ python3 server.py
INFO:root:Server serving at [::]:50051
INFO:root:Received a phone call request for number [1415926535]
INFO:root:Created a call session [{
  "sessionId": "0",
  "media": "https://link.to.audio.resources"
}]
INFO:root:Call finished [1415926535]
INFO:root:Call session cleaned [{
  "sessionId": "0",
  "media": "https://link.to.audio.resources"
}]
$ python3 client.py
INFO:root:Waiting for peer to connect [1415926535]...
INFO:root:Call toward [1415926535] enters [NEW] state
INFO:root:Call toward [1415926535] enters [ACTIVE] state
INFO:root:Consuming audio resource [https://link.to.audio.resources]
INFO:root:Call toward [1415926535] enters [ENDED] state
INFO:root:Audio session finished [https://link.to.audio.resources]
INFO:root:Call finished!