-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_stream.py
44 lines (30 loc) · 880 Bytes
/
client_stream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import os
import time
import grpc
from loguru import logger
import ping_pb2
import ping_pb2_grpc
MESSAGES = [
"I like shiny things",
"But I will marry you",
"with paper rings",
]
def message_iter():
for message in MESSAGES:
yield ping_pb2.Ping(client_message=message)
time.sleep(2)
def run():
message: str = ""
pid = os.getpid()
with grpc.insecure_channel("[::]:8080") as channel:
stub = ping_pb2_grpc.PingPongServiceStub(channel)
try:
response = stub.client_stream(message_iter())
message_from_server = response.num_pings
logger.info(f"Server said there were {message_from_server} messages")
except KeyboardInterrupt as ki:
logger.critical("Bye!")
channel.unsubscribe(channel.close())
exit()
if __name__ == "__main__":
run()