grpc/examples/python/async_streaming
Lidi Zheng 2231c2ba77
Introduce Python import sorting to our sanity test suite (#26768)
* Add isort_code.sh to sanity tests

* Run tools/distrib/isort_code.sh

* Fine tune the import order for relative imports

* Make pylint and project generation happy

* Fix a few corner cases

* Use --check instead of --diff

* The import order impacts test result somehow

* Make isort print diff and check output at the same time

* Let tools/run_tests/python_utils be firstparty library

* Run isort against latest HEAD
2021-07-26 12:31:21 -07:00
..
README.md Add async bidi-streaming example 2020-12-21 14:38:39 -08:00
client.py Introduce Python import sorting to our sanity test suite (#26768) 2021-07-26 12:31:21 -07:00
phone.proto Add async bidi-streaming example 2020-12-21 14:38:39 -08:00
phone_pb2.py Add async bidi-streaming example 2020-12-21 14:38:39 -08:00
phone_pb2_grpc.py Add async bidi-streaming example 2020-12-21 14:38:39 -08:00
server.py Introduce Python import sorting to our sanity test suite (#26768) 2021-07-26 12:31:21 -07:00

README.md

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!