-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
49 lines (39 loc) · 1.27 KB
/
Copy pathserver.py
File metadata and controls
49 lines (39 loc) · 1.27 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
# Library imports
from concurrent import futures
import grpc
import logging
import time
# Local imports
from matches_pb2 import CareerMatch
from matches_pb2 import CareerMatchesResponse
import matches_pb2_grpc
# Logging config
logging.basicConfig(level='DEBUG')
logger = logging.getLogger(__name__)
class CareerMatchesServiceServicer(matches_pb2_grpc.CareerMatchesServiceServicer):
"""
Implementation of the service in python.
"""
def GetMatches(self, request, context):
matches = [
CareerMatch(career_id=1, score=5.0),
CareerMatch(career_id=2, score=4.5),
CareerMatch(career_id=3, score=2.0),
CareerMatch(career_id=4, score=1.0),
]
return CareerMatchesResponse(matches=matches)
def serve():
logger.info('serve() running')
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
matches_pb2_grpc.add_CareerMatchesServiceServicer_to_server(CareerMatchesServiceServicer(), server)
server.add_insecure_port('[::]:50051')
server.start()
# Hang while we serve
try:
while True:
time.sleep(60 * 60 * 24)
except KeyboardInterrupt:
logger.info('serve() interrupted by keyboard input')
server.stop(0)
if __name__ == '__main__':
serve()