This repository has been archived by the owner on Mar 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
grpc_server.py
57 lines (44 loc) · 1.77 KB
/
grpc_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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
from concurrent import futures
import time
import argparse
from sklearn.externals import joblib
import grpc
import iris_pb2
import iris_pb2_grpc
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
class IrisPredictor(iris_pb2_grpc.IrisPredictorServicer):
_model = None
@classmethod
def get_or_create_model(cls):
"""
Get or create iris classification model.
"""
if cls._model is None:
path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'model', 'iris_model.pickle')
cls._model = joblib.load(path)
return cls._model
def PredictIrisSpecies(self, request, context):
model = self.__class__.get_or_create_model()
sepal_length = request.sepal_length
sepal_width = request.sepal_width
petal_length = request.petal_length
petal_width = request.petal_width
result = model.predict([[sepal_length, sepal_width, petal_length, petal_width]])
return iris_pb2.IrisPredictReply(species=result[0])
def serve(port, max_workers):
server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers))
iris_pb2_grpc.add_IrisPredictorServicer_to_server(IrisPredictor(), server)
server.add_insecure_port('[::]:{port}'.format(port=port))
server.start()
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--port', type=int, help='port number', required=False, default=50052)
parser.add_argument('--max_workers', type=int, help='# max workers', required=False, default=10)
args = parser.parse_args()
serve(port=args.port, max_workers=args.max_workers)