-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
56 lines (41 loc) · 1.59 KB
/
server.py
File metadata and controls
56 lines (41 loc) · 1.59 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
import grpc
import time
from concurrent import futures
import fibonacci_pb2
import fibonacci_pb2_grpc
ONE_HOUR_IN_SECONDS = 3600
# usually iterative version has less overhead than recursive version,
# because you can control what info to put into the iterative stack,
# while the recursive stack (return address, local variables, etc.) is handled implicitly.
def iterative_fibonacci(n):
if n <= 1:
return n
cache = [0, 1]
for _ in range(1, n):
cache[1], cache[0] = cache[0] + cache[1], cache[1]
return cache[1]
class FibonacciServicer(fibonacci_pb2_grpc.FibonacciServicer):
def __init__(self, port=6000):
self.port = port
# implementation of the Fibonacci service declared in the proto file
def Fibonacci(self, request, context):
response = fibonacci_pb2.FibonacciMessage()
response.value = iterative_fibonacci(request.value)
return response
def start_server(self):
# create a gRPC server
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
fibonacci_pb2_grpc.add_FibonacciServicer_to_server(FibonacciServicer(), server)
# bind the server to the specified port
server.add_insecure_port('[::]:{}'.format(self.port))
server.start()
print('Server started. \nListening on port {}.'.format(self.port))
# keep the server alive
try:
while True:
time.sleep(ONE_HOUR_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__':
server = FibonacciServicer()
server.start_server()