| 1 |
# gRPC Python Non-Blocking Streaming RPC Client Example |
| 2 |
|
| 3 |
The goal of this example is to demonstrate how to handle streaming responses |
| 4 |
without blocking the current thread. Effectively, this can be achieved by |
| 5 |
converting the gRPC Python streaming API into callback-based. |
| 6 |
|
| 7 |
In this example, the RPC service `Phone` simulates the life cycle of virtual |
| 8 |
phone calls. It requires one thread to handle the phone-call session state |
| 9 |
changes, and another thread to process the audio stream. In this case, the |
| 10 |
normal blocking style API could not fulfill the need easily. Hence, we should |
| 11 |
asynchronously execute the streaming RPC. |
| 12 |
|
| 13 |
## Steps to run this example |
| 14 |
|
| 15 |
Start the server in one session |
| 16 |
``` |
| 17 |
python3 server.py |
| 18 |
``` |
| 19 |
|
| 20 |
Start the client in another session |
| 21 |
``` |
| 22 |
python3 client.py |
| 23 |
``` |
| 24 |
|
| 25 |
## Example Output |
| 26 |
``` |
| 27 |
$ python3 server.py |
| 28 |
INFO:root:Server serving at [::]:50051 |
| 29 |
INFO:root:Received a phone call request for number [1415926535] |
| 30 |
INFO:root:Created a call session [{ |
| 31 |
"sessionId": "0", |
| 32 |
"media": "https://link.to.audio.resources" |
| 33 |
}] |
| 34 |
INFO:root:Call finished [1415926535] |
| 35 |
INFO:root:Call session cleaned [{ |
| 36 |
"sessionId": "0", |
| 37 |
"media": "https://link.to.audio.resources" |
| 38 |
}] |
| 39 |
``` |
| 40 |
|
| 41 |
``` |
| 42 |
$ python3 client.py |
| 43 |
INFO:root:Waiting for peer to connect [1415926535]... |
| 44 |
INFO:root:Call toward [1415926535] enters [NEW] state |
| 45 |
INFO:root:Call toward [1415926535] enters [ACTIVE] state |
| 46 |
INFO:root:Consuming audio resource [https://link.to.audio.resources] |
| 47 |
INFO:root:Call toward [1415926535] enters [ENDED] state |
| 48 |
INFO:root:Audio session finished [https://link.to.audio.resources] |
| 49 |
INFO:root:Call finished! |
| 50 |
``` |