【发布时间】:2017-12-06 20:31:04
【问题描述】:
我正在开发一个简单的 Flask 应用程序,该应用程序最终将变成一个简单的 REST API,用于在给定的文本字符串上使用 spaCy 进行命名实体识别。我有一个简单的原型如下:
from flask import Flask, render_template, request, json
import spacy
from spacy import displacy
def to_json(doc):
return [
{
'start': ent.start_char,
'end': ent.end_char,
'type': ent.label_,
'text': str(ent),
} for ent in doc.ents
]
nlp = spacy.load('en')
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/demo', methods=['GET', 'POST'])
def demo():
q = request.values.get('text')
doc = nlp(q)
if request.values.get('type') == 'html':
return displacy.render(doc, style='ent', page=True)
else:
return app.response_class(
response=json.dumps(to_json(doc), indent=4),
status=200,
mimetype='text/string'
)
if __name__ == '__main__':
app.run(host='0.0.0.0')
Flask 应用程序使用 Ubuntu 上的 Apache 网络服务器提供服务。我使用一个简单的 Web 表单向应用程序提交文本,它以 HTML 或 JSON 文本的形式返回结果。
我遇到的问题是应用程序间歇性挂起......我无法弄清楚导致它挂起的模式。 Apache 错误日志中没有显示任何内容,挂起的请求也不会出现在 Apache 访问日志中。如果我在浏览器旋转时终止服务器,浏览器会报告服务器提供了一个空响应。如果我重新启动服务器,错误日志会报告 1 或 2 个子进程在 SIGTERM 之后没有退出,并且必须发送 SIGKILL。
一个可能的线索是服务器启动时错误日志报告如下:
[Wed Dec 06 20:19:33.753041 2017] [wsgi:warn] [pid 1822:tid 140029812619136] mod_wsgi: Compiled for Python/2.7.11.
[Wed Dec 06 20:19:33.753055 2017] [wsgi:warn] [pid 1822:tid 140029812619136] mod_wsgi: Runtime using Python/2.7.12.
另一个可能的线索是“索引”路由 (/) 似乎永远不会挂起。但是“/demo”路由对于request.values.get('type') == 'html'if 语句的两个分支都可能挂起。
编辑: 我已将 Apache 和 mod_wsgi 排除在外,现在正在使用独立的 Flask 服务器运行该应用程序。该应用程序仍然偶尔挂起...当它挂起时,我可以按 control-c 并始终返回以下作为最新代码:
Exception happened during processing of request from ('xxx.xxx.xxx.xxx', 55608)
Traceback (most recent call last):
File "/usr/lib/python2.7/SocketServer.py", line 290, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 318, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python2.7/SocketServer.py", line 331, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python2.7/SocketServer.py", line 652, in __init__
self.handle()
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 232, in handle
rv = BaseHTTPRequestHandler.handle(self)
File "/usr/lib/python2.7/BaseHTTPServer.py", line 340, in handle
self.handle_one_request()
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 263, in handle_one_request
self.raw_requestline = self.rfile.readline()
File "/usr/lib/python2.7/socket.py", line 451, in readline
data = self._sock.recv(self._rbufsize)
KeyboardInterrupt
----------------------------------------
按下 control-c 后,Flask 得到“释放”,然后返回我期望的结果。服务器继续正常运行,并将接受更多请求,直到再次挂起。有时,如果我等待足够长的时间,挂起的请求会自行返回。
这似乎越来越像是 Flask 的问题(或者我是如何使用它的)。如果有人可以提供有关如何追踪问题的建议,我将不胜感激!
【问题讨论】:
-
我猜这个任务对于一个网络应用来说太重了。如果在交互式烧瓶会话中逐步运行 NLP 内容需要多长时间?
-
当应用响应成功时,速度相当快……不到一秒。我没有尝试过交互式 Flask 会话,但是使用常规交互式 python 会话 spacy.load('en') (加载英语的 spacy 模型)大约需要 1 秒。 “doc = nlp(q)”步骤非常快。我不清楚 Flask 是否会在每个请求上加载 spacy 模型......如果是这样,那么在模型完成加载之前是否有可能正在处理“路线”?
标签: python apache flask mod-wsgi spacy