Commit 5b6ce49d by lichenggang

format

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