forked from ramananbalakrishnan/basic-grpc-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
28 lines (21 loc) · 738 Bytes
/
Copy pathclient.py
File metadata and controls
28 lines (21 loc) · 738 Bytes
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
import asyncio
import grpc
# import the generated classes
import calculator_pb2
import calculator_pb2_grpc
from functools import partial
def get_call(loop, number):
# open a gRPC channel
channel = grpc.insecure_channel('localhost:50051')
# create a stub (client)
stub = calculator_pb2_grpc.CalculatorStub(channel)
number = calculator_pb2.Number(value=number)
response = stub.SquareRoot(number)
print(response)
return response
loop = asyncio.get_event_loop()
# tasks = asyncio.gather(get_call(loop, 5), get_call(loop, 4))
response = loop.run_in_executor(None, partial(get_call, loop, 9))
response = loop.run_in_executor(None, partial(get_call, loop, 4))
loop.run_until_complete(response)
# et voilà