Commit e1f97998 by lichenggang

临时增加http服务

parent ebf97191
Showing with 91 additions and 0 deletions
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import json
from urllib.parse import quote
import tornado.web
import tornado.ioloop
import traceback
from utils.config import model_config
from predic_fac import PredictorFac
from utils.log_manager import get_logger
log_server = get_logger('httpserver')
class BaseHandler(tornado.web.RequestHandler):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
log_server.info('http server start!')
self.predictorfac = PredictorFac(model_config)
class KwHandler(BaseHandler):
async def get(self):
target = quote(self.get_argument('keyword'))
res = self.predictorfac.predict(target)
res['status'] = 1
self.write(res)
class DictHandler(BaseHandler):
async def post(self):
try:
dic_data = json.loads(self.request.body)
res = self.predictorfac.predict(dic_data)
res['status'] = 1
except json.decoder.JSONDecodeError:
res = self.predictorfac.predict(self.request.body)
res['status'] = 1
except :
res = {
'status': 0,
'err_msg': '未知'
}
log_server.error(traceback.format_exc())
self.write(res)
def gen_app():
return tornado.web.Application(handlers=[(k, v) for k, v in register_tornado_handlers.items()])
register_tornado_handlers = {'/kw': KwHandler, '/excel': DictHandler}
if __name__ == '__main__':
port = 50052
app = gen_app()
print(f'kw监听端口:{port}')
app.listen(port)
tornado.ioloop.IOLoop.current().start()
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import tornado
from classify_server import serve
from http_server import gen_app
from multiprocessing import Process
def run_http():
port = 50052
app = gen_app()
print(f'kw监听端口:{port}')
app.listen(port)
tornado.ioloop.IOLoop.current().start()
def run_grpc():
serve()
def run():
p_http = Process(target=run_http)
p_grpc = Process(target=run_grpc)
p_http.daemon = True
p_grpc.daemon = True
p_http.start()
p_grpc.start()
p_http.join()
p_grpc.join()
if __name__ == "__main__":
run()
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment