forked from louiezzang/faiss-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
123 lines (105 loc) · 3.02 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import time
import logging
import os
import sys
import signal
import argparse
from concurrent import futures
import grpc
import faiss_pb2_grpc as pb2_grpc
from faiss_server import FaissServer
_ONE_DAY_IN_SECONDS = 60 * 60 * 24
if not os.path.exists(os.getcwd() + "/logs"):
os.makedirs(os.getcwd() + "/logs")
def main(args):
dim = args.dim
save_path = args.save_path
keys_path = args.keys_path
log = args.log
debug = args.debug
no_save = args.no_save
max_workers = args.max_workers
num_threads = args.num_threads
nprobe = args.nprobe
if log:
handler = logging.FileHandler(filename=log)
else:
handler = logging.StreamHandler(sys.stdout)
formatter = logging.Formatter("%(asctime)s:%(levelname)s:%(name)s - %(message)s")
handler.setFormatter(formatter)
root = logging.getLogger()
level = debug and logging.DEBUG or logging.INFO
root.setLevel(level)
root.addHandler(handler)
logging.info("server loading...")
logging.info("max_workers: %d", max_workers)
if num_threads is not None:
logging.info("num_threads: %d", num_threads)
server = grpc.server(futures.ThreadPoolExecutor(max_workers=max_workers))
servicer = FaissServer(dim, save_path, keys_path, nprobe)
pb2_grpc.add_ServerServicer_to_server(servicer, server)
server.add_insecure_port("[::]:50051")
server.start()
logging.info("server started")
# for docker heath check
with open("/tmp/status", "w") as f:
f.write("started")
def stop_serve(signum, frame):
raise KeyboardInterrupt
signal.signal(signal.SIGINT, stop_serve)
signal.signal(signal.SIGTERM, stop_serve)
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
if not no_save:
servicer.save()
logging.info("server stopped")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
'--dim',
type=int,
default=200,
help='Dimension')
parser.add_argument(
'--save_path',
type=str,
default='faiss_server.index',
help='index save path')
parser.add_argument(
'--keys_path',
type=str,
help='keys file path')
parser.add_argument(
'--log',
type=str,
help='log file path')
parser.add_argument(
'--debug',
type=bool,
default=True,
help='debug')
parser.add_argument(
'--no_save',
type=bool,
default=True,
help='no save when stop service')
parser.add_argument(
'--max_workers',
type=int,
default=1,
help='grpc workers count')
parser.add_argument(
'--num_threads',
type=int,
default=None,
help='faiss index thread count(omp_set_num_threads)')
parser.add_argument(
'--nprobe',
type=int,
default=1,
help='nprobe for the search quality')
args = parser.parse_args()
main(args)