Commit 5b6ce49d by lichenggang

format

parent b201e8e8
from concurrent import futures from concurrent import futures
import time, json import time, json
import grpc import grpc
from utils.log_manager import bom_log
from protobuf import classify_pb2 from protobuf import classify_pb2
from protobuf import classify_pb2_grpc from protobuf import classify_pb2_grpc
import traceback import traceback
from utils.config import model_config from utils.config import model_config
from predic_fac import PredictorFac from predic_fac import PredictorFac
from utils.log_manager import get_logger
log_server = get_logger('server')
class Classify(classify_pb2_grpc.classifyServicer): class Classify(classify_pb2_grpc.classifyServicer):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
log_server.info('classify server start!')
self.predictorfac = PredictorFac(model_config) self.predictorfac = PredictorFac(model_config)
def Classify(self, request, context): def Classify(self, request, context):
bom_log.info(f'grpc收到数据: {request.keyword}')
try: try:
dic_data = json.loads(request.keyword) dic_data = json.loads(request.keyword)
res = self.predictorfac.predict(dic_data) res = self.predictorfac.predict(dic_data)
...@@ -25,12 +24,12 @@ class Classify(classify_pb2_grpc.classifyServicer): ...@@ -25,12 +24,12 @@ class Classify(classify_pb2_grpc.classifyServicer):
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError:
res = self.predictorfac.predict(request.keyword) res = self.predictorfac.predict(request.keyword)
res['status'] = 1 res['status'] = 1
except : except:
res = { res = {
'status': 0, 'status': 0,
'err_msg': '未知' 'err_msg': '未知'
} }
log_server.error(traceback.format_exc()) bom_log.error(f'grpc报错: \n' + traceback.format_exc())
return classify_pb2.ClassifyReply(message='result {msg}'.format(msg=res)) return classify_pb2.ClassifyReply(message='result {msg}'.format(msg=res))
def fac_test_predic(self, data): def fac_test_predic(self, data):
...@@ -38,11 +37,11 @@ class Classify(classify_pb2_grpc.classifyServicer): ...@@ -38,11 +37,11 @@ class Classify(classify_pb2_grpc.classifyServicer):
return res return res
def serve(): def grpc_server(port):
# 启动 rpc 服务 # 启动 rpc 服务
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
classify_pb2_grpc.add_classifyServicer_to_server(Classify(), server) classify_pb2_grpc.add_classifyServicer_to_server(Classify(), server)
server.add_insecure_port('[::]:50051') server.add_insecure_port(f'[::]:{port}')
server.start() server.start()
try: try:
while True: while True:
...@@ -52,11 +51,10 @@ def serve(): ...@@ -52,11 +51,10 @@ def serve():
if __name__ == '__main__': if __name__ == '__main__':
serve() port = 50051
grpc_server(port)
# data = read_from_excel('DZ0901_V1.4_BOM.xlsx', 'DZ0901_V1.3BOM清单') # data = read_from_excel('DZ0901_V1.4_BOM.xlsx', 'DZ0901_V1.3BOM清单')
# print(data) # print(data)
# print(Classify().fac_test_predic(data)) # print(Classify().fac_test_predic(data))
# for i in range(65, 91): # for i in range(65, 91):
# print(chr(i)) # print(chr(i))
...@@ -6,49 +6,66 @@ from urllib.parse import unquote ...@@ -6,49 +6,66 @@ from urllib.parse import unquote
import tornado.web import tornado.web
import tornado.ioloop import tornado.ioloop
import traceback import traceback
from utils.log_manager import bom_log
from utils.config import model_config from utils.config import model_config
from predic_fac import PredictorFac from predic_fac import PredictorFac
from utils.log_manager import get_logger
log_server = get_logger('httpserver')
class BaseHandler(tornado.web.RequestHandler): class BaseHandler(tornado.web.RequestHandler):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
log_server.info('http server start!')
self.predictorfac = PredictorFac(model_config) self.predictorfac = PredictorFac(model_config)
class KwHandler(BaseHandler): class KwHandler(BaseHandler):
async def get(self): async def get(self):
try:
target = unquote(self.get_argument('keyword')) target = unquote(self.get_argument('keyword'))
bom_log.info(f'http收到识别关键词: {target}')
res = self.predictorfac.predict(target) res = self.predictorfac.predict(target)
res['status'] = 1 res['status'] = 1
except:
res = {
'status': 0,
'err_msg': '未知'
}
bom_log.error(f'http关键词报错: \n' + traceback.format_exc())
self.write(res) self.write(res)
class DictHandler(BaseHandler): class DictHandler(BaseHandler):
async def post(self): async def post(self):
try: try:
bom_log.info(f'http收到识别字典: {self.request.body}')
dic_data = json.loads(self.request.body) dic_data = json.loads(self.request.body)
res = self.predictorfac.predict(dic_data) res = self.predictorfac.predict(dic_data)
res['status'] = 1 res['status'] = 1
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError:
res = self.predictorfac.predict(self.request.body) res = self.predictorfac.predict(self.request.body)
res['status'] = 1 res['status'] = 1
except : except:
res = { res = {
'status': 0, 'status': 0,
'err_msg': '未知' 'err_msg': '未知'
} }
log_server.error(traceback.format_exc()) bom_log.error(f'http字典报错: \n' + traceback.format_exc())
self.write(res) self.write(res)
def gen_app(): def gen_app():
return tornado.web.Application(handlers=[(k, v) for k, v in register_tornado_handlers.items()]) 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__': register_tornado_handlers = {'/kw': KwHandler, '/dict': DictHandler}
port = 50052
def http_server(port):
app = gen_app() app = gen_app()
print(f'kw监听端口:{port}')
app.listen(port) app.listen(port)
tornado.ioloop.IOLoop.current().start() tornado.ioloop.IOLoop.current().start()
if __name__ == '__main__':
port = 50052
http_server(port)
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
import tornado from classify_server import grpc_server
from http_server import http_server
from classify_server import serve
from http_server import gen_app
from multiprocessing import Process from multiprocessing import Process
from utils.log_manager import bom_log
def run_http(): def run(http_port, grpc_port):
port = 50052 bom_log.info(f'http server 启动!监听端口:{http_port}')
app = gen_app() bom_log.info(f'grpc server 启动!监听端口:{grpc_port}')
print(f'kw监听端口:{port}') p_http = Process(target=http_server, args=(http_port,))
app.listen(port) p_grpc = Process(target=grpc_server, args=(grpc_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_http.start()
p_grpc.start() p_grpc.start()
p_http.join()
p_grpc.join()
if __name__ == "__main__": if __name__ == "__main__":
run() http_port = 50052
\ No newline at end of file grpc_port = 50051
run(http_port, grpc_port)
...@@ -37,3 +37,5 @@ def get_logger(log_name, log_type='file'): ...@@ -37,3 +37,5 @@ def get_logger(log_name, log_type='file'):
create_logger(log_name, log_type) create_logger(log_name, log_type)
logger_dict[log_name] = logging.getLogger(log_name) logger_dict[log_name] = logging.getLogger(log_name)
return logging.getLogger(log_name) return logging.getLogger(log_name)
bom_log = get_logger('bomserver')
\ 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