-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
38 lines (31 loc) · 1.04 KB
/
server.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
from concurrent import futures
import grpc
import pingpong_pb2
import pingpong_pb2_grpc
import time
import threading
class Listener(pingpong_pb2_grpc.PingPongServiceServicer):
def __init__(self) -> None:
self.counter = 0
self.last_print_time = time.time()
def ping(self, request, context):
self.counter += 1
if self.counter > 10000:
print("10000 calls in %3f" % (time.time() - self.last_print_time))
self.last_print_time = time.time()
self.counter = 0
return pingpong_pb2.Pong(count=request.count + 1)
def serve():
server = grpc.server(futures.ThreadPoolExecutor(max_workers=1))
pingpong_pb2_grpc.add_PingPongServiceServicer_to_server(Listener(), server)
server.add_insecure_port("[::]:9999")
server.start()
try:
while True:
print("server on: threads %i" % (threading.active_count()))
time.sleep(10)
except KeyboardInterrupt:
print("KeyboardInterrupt")
server.stop(0)
if __name__ == "__main__":
serve()