使用了flask + hanlp2.1封装成分词接口服务,是可以正常启动的,但当封装到gunicorn时,启动服务后
语句:python -m gunicorn -w 8 app:app -c gunicorn.conf.py
就一直卡在Building model…这里。有哪位大神知道怎么处理吗
app.py如下:
import os
import hanlp
import json
import logging
from flask import Flask, request, jsonify, current_app as app, Response
app = Flask(name)
os.environ[“CUDA_VISIBLE_DEVICES”] = “0,1”
tokenizer = hanlp.load(hanlp.pretrained.tok.COARSE_ELECTRA_SMALL_ZH)
tok_fine = hanlp.load(hanlp.pretrained.tok.FINE_ELECTRA_SMALL_ZH)
@app.route("/hanlp", methods=[“GET”, “POST”])
def hlp():
if request.method == "GET":
return jsonify({
"status": "not ok",
"message": "Get Method Is Not Allowed."
}), 405 # HTTP status code for Method Not Allowed
# Retrieve options
options = request.get_json(silent=True) or {}
text = options.get("text", "")
method = options.get("method", "")
app.logger.debug(text)
HanLP = hanlp.pipeline().append(hanlp.utils.rules.split_sentence).append(
tok_fine if method == "fine" else tokenizer
).append(lambda sents: sum(sents, []))
result = HanLP(text)
return Response(
response=json.dumps(result, ensure_ascii=False),
status=200, mimetype="application/json; charset=utf-8"
)
@app.after_request
def after_request(response):
app.logger.debug(response.get_json(silent=True))
return response
if name == ‘main’:
app.run(host="0.0.0.0", port=5000, debug=True)
gunicorn.conf.py配置文件如下:
import os
import multiprocessing
path_of_current_file = os.path.abspath(file)
path_of_current_dir = os.path.split(path_of_current_file)[0]
chdir = path_of_current_dir
workers = 1 # 进程数量
threads = 2
worker_connections = 2000
pidfile = ‘%s/gunicorn.pid’ % path_of_current_dir # 存放Gunicorn进程pid的位置,便于跟踪
accesslog = ‘%s/log/00_gunicorn_access.log’ % path_of_current_dir # 存放访问日志的位置,注意首先需要存在logs文件夹,Gunicorn才可自动创建log文件
errorlog = ‘%s/log/00_gunicorn_access.log’ % path_of_current_dir # 存放错误日志的位置,可与访问日志相同
reload = True # 如果应用的代码有变动,work将会自动重启,适用于开发阶段
debug = False
timeout = 500 # server端的请求超时秒数
loglevel = ‘error’