/
client.py
35 lines (28 loc) · 963 Bytes
/
client.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
import messages_pb2
import messages_pb2_grpc
import grpc
import time
import random
from datetime import datetime
from os import environ
def main():
address = environ.get('ADDRESS', 'localhost:50051')
print(f"address: {address}")
# sleep for a random amount of time to stagger requests with other clients
random.seed()
sleep_time = random.randint(0, 3)
print(f"sleeping for {sleep_time}")
time.sleep(sleep_time)
request = messages_pb2.Request(start=0, end=100)
with grpc.insecure_channel(address) as channel:
stub = messages_pb2_grpc.RandomNumberGeneratorStub(channel)
while True:
try:
n = stub.GetRandomNumber(request)
except grpc.RpcError as e:
print(f"{datetime.now()} ERROR {e.code()}: {e.details()}")
else:
print(f"{datetime.now()} Received: {n.num}")
time.sleep(3)
if __name__ == '__main__':
main()