-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.py
More file actions
318 lines (274 loc) · 11 KB
/
main.py
File metadata and controls
318 lines (274 loc) · 11 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
# Copyright 2019 The Vearch Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# ==============================================================================
"""``main.py`` is the entry of programs, you can run ``python main.py``.
Options:
--port Define the port your server run on
--gpu Define the GPU your server run on
--model_name Define the model you need
"""
import os
import json
import queue
import time
import signal
import requests
import threading
import shortuuid
import argparse
import traceback
import concurrent.futures
from multiprocessing import Queue, Process
from asyncio.futures import wrap_future
import tornado
import tornado.web
import tornado.httpserver
from tornado.httputil import HTTPServerRequest
from typing import Dict, List, Union
from tornado.options import define, options
import config
import util
import controller
_INSERT = '_insert'
_SEARCH = '_search'
_HEADERS = {'content-type': 'application/json'}
# InvalidURL = requests.InvalidURL
class PackageProcess(Process):
"""The Process dealing request and return result."""
def __init__(self,
gpu_id: str,
model_name: str,
input_queue: Queue,
output_queue: Queue,
debug: bool = False
) -> None:
Process.__init__(self)
self.gpu_id = gpu_id # The GPU id of model run on
self.model_name = model_name # image_retrieval or face_retrieval
self.model = None
self.debug = debug
self.input_queue = input_queue
self.output_queue = output_queue
self.watch_queue = queue.Queue() # thread queue for performance
def build(self):
"""Initialization process environment.
1.set visible GPU;
2.load defined model
"""
os.environ['CUDA_VISIBLE_DEVICES'] = self.gpu_id
self.daemon_thread()
self.model = controller.load_model(self.model_name)
@staticmethod
def daemon_thread():
"""Define a daemon thread listen the status of parent process."""
def kill():
while True:
if os.getppid() == 1:
os.kill(os.getpid(), signal.SIGKILL)
time.sleep(1)
t = threading.Thread(target=kill)
t.start()
@staticmethod
def judge(uri_list: List[str], request: 'Request') -> None:
"""Raise a Exception when create a db that name in [_cluster, list, db, space]
:param uri_list: The list of uri.
:param request: The dict of request body.
:return None
:raise Exception
"""
if uri_list[1] == 'db' and uri_list[2] == '_create':
if not request.body:
raise requests.RequestException('The request data can not be empty!')
data = json.loads(request.body, encoding='utf8')
if 'name' not in data:
raise requests.RequestException('The name of db is required in request data!')
if data['name'] in ['_cluster', 'list', 'db', 'space']:
raise requests.RequestException('The name of db can not in [_cluster, list, db, space]!')
def deal(self, uuid: str, request: 'Request') -> None:
"""Deal the request.
Repackage the request body and send it to VectorDB and put the response into `response_queue`
:param uuid: The uuid of request.
:param request: The request object.
:return None
"""
try:
uri_list = request.uri.split('?')[0].split('/')
if len(uri_list) < 3:
raise requests.RequestException('Bad Request, Page not Found.')
elif uri_list[1] in ['_cluster', 'list', 'db', 'space']:
self.judge(uri_list, request)
ip = f'{config.master_address}{request.uri}'
elif request.method == 'POST':
self.post(uri_list, request)
ip = f'{config.router_address}{request.uri}'
else:
ip = f'{config.router_address}{request.uri}'
res = requests.request(request.method, ip, data=request.body, headers=request.headers)
result = Response(res)
self.output_queue.put((uuid, True, result))
except Exception as err:
if self.debug:
traceback.print_exc()
self.output_queue.put((uuid, False, str(err)))
def post(self, uri_list: List[str], request: 'Request') -> None:
"""Extract feature in request if necessary.
:param uri_list: The list of uri.
:param request: The dict of request body.
:return None
:raise NotImplementedError
"""
flag = False
operate = uri_list[3] if len(uri_list) >= 4 else None
data = json.loads(request.body, encoding='utf8')
if operate == _SEARCH:
for d in data['query']['sum']:
if not isinstance(d['feature'], list):
flag = True
d['feature'] = util.normlize(self.model.encode(d['feature']))
elif operate == '_msearch':
raise NotImplementedError('This API do not support msearch operate')
else:
for key in data:
if isinstance(data[key], dict) and 'feature' in data[key]:
field = data[key]['feature']
if not isinstance(field, list):
flag = True
data[key]['feature'] = util.normlize(self.model.encode(field))
if flag:
# if not extract feature, send the request to server directly.
request.body = json.dumps(data, ensure_ascii=False).encode('utf8')
def run(self):
"""The entry to program start."""
self.build()
print('load model success')
while True:
uuid, request = self.input_queue.get()
self.deal(uuid, request)
class TableHandler(tornado.web.RequestHandler):
"""The server of receive and deal request."""
def initialize(self, input_queue: Queue,
futures_dict: Dict[str, concurrent.futures.Future]
) -> None:
"""Initialize the request, called for each request.
:param input_queue: The request queue.
:param futures_dict: The future dict store the uuid of request and future.
"""
self.input_queue = input_queue
self.futures_dict = futures_dict
def write(self, result: Union[str, 'Response']):
if isinstance(result, Response):
code, chunk = result.status_code, result.text
else:
code, chunk = 551, result
self.set_status(code, reason=chunk)
# chunk = json.dumps(result, ensure_ascii=False)
# status = result.get('status', 0) or result.get('code', 0)
# if status:
# self.set_status(status, reason=chunk)
super(TableHandler, self).write(chunk)
async def get(self):
response = await self.deal()
self.write(response)
async def delete(self):
response = await self.deal()
self.write(response)
async def put(self):
response = await self.deal()
self.write(response)
async def post(self):
response = await self.deal()
self.write(response)
async def deal(self):
uuid = shortuuid.uuid()
future = concurrent.futures.Future()
self.futures_dict[uuid] = future
self.input_queue.put((uuid, Request(self.request)))
result = await wrap_future(future)
return result
class Request(dict):
def __init__(self, a: HTTPServerRequest):
super(Request, self).__init__()
self.method = a.method
self.uri = a.uri
self.headers = a.headers
self.body = a.body
def __str__(self):
return f'{self.__class__.__name__}({self.__dict__})'
def __repr__(self):
return self.__str__()
class Response(dict):
def __init__(self, a: requests.Response):
super(Response, self).__init__()
self.url = a.url
self.text = a.text
self.elapsed = a.elapsed
self.status_code = a.status_code
def __str__(self):
return f'{self.__class__.__name__}({self.__dict__})'
def __repr__(self):
return self.__str__()
def set_result_thread(result_queue: Queue,
futures_dict: Dict[str, concurrent.futures.Future]
) -> None:
"""Assign results asynchronously.
:param result_queue: The queue of result which put by ``PackageProcess``.
:param futures_dict: The dict which save future of each request.
:return None.
"""
while True:
# uuid is the id of request.
# flag is the status of request, True is success other is failed.
# result is the result of request, if flag is false, result is the error info of request.
uuid, flag, result = result_queue.get()
if uuid in futures_dict:
futures_dict[uuid].set_result(result)
del futures_dict[uuid]
def run(port, url_queue, result_queue):
tornado.options.parse_command_line()
futures_dict = dict()
t1 = threading.Thread(target=set_result_thread, args=(result_queue, futures_dict))
t1.start()
app = tornado.web.Application(
[(f'/.*', TableHandler, dict(input_queue=url_queue, futures_dict=futures_dict))]
)
sockets = tornado.netutil.bind_sockets(port)
server = tornado.httpserver.HTTPServer(app)
server.add_sockets(sockets)
tornado.ioloop.IOLoop.instance().start()
def install(model_name):
if model_name == 'face_retrieval':
util.install_package('tensorflow-gpu==1.15.2 mtcnn keras==2.2.4')
elif model_name == 'image_retrieval':
util.install_package('torchvision torch')
elif model_name == 'text':
text_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'text', 'requirements.txt')
util.install_package(f'-r {text_path}')
elif model_name == 'audio':
raise NotImplementedError()
else:
raise Exception(f'{model_name} is not existed')
def main():
define('model_name', type=str, default='face_retrieval', help='The model name you need')
define('debug', type=bool, default=False, help='debug')
tornado.options.parse_command_line()
install(options.model_name)
port = config.port
gpus = config.gpus
url_queue = Queue()
result_queue = Queue()
for gpu in gpus.split(','):
package_process = PackageProcess(gpu, options.model_name, url_queue, result_queue, debug=options.debug)
package_process.daemon = True
package_process.start()
run(port, url_queue, result_queue)
if __name__ == '__main__':
main()