python - 使用 spaCy NLP 的简单 Flask 应用程序间歇性挂起

标签 python apache flask mod-wsgi spacy

我正在开发一个简单的 Flask 应用程序,它最终会变成一个简单的 REST API,用于在给定的文本字符串上使用 spaCy 进行命名实体识别。我有一个简单的原型(prototype)如下:

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 服务器提供服务。我使用简单的 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 的问题(或者我如何使用它)。如果有人可以提供有关如何查找问题的建议,我将不胜感激!

最佳答案

尝试强制用户使用主 Python 解释器上下文,如下所述:

Python 中的某些第三方 C 扩展模块无法在子解释器中正常工作,并且可能会挂起或崩溃进程。

关于python - 使用 spaCy NLP 的简单 Flask 应用程序间歇性挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47682966/

相关文章:

python - 执行命令时出现pymysql.err.ProgrammingError 1064

python - sklearn 的 make_blobs 和多元高斯有什么区别?

java - 如何使用 Apache POI 从 Excel 文件中获取列?

python - Flask 重定向(相同的路由,不同的 HTTP 方法)最终出现 302 循环

javascript - 将 Javascript 变量插入到 Flask 应用程序中,然后将其插入到 Mysql 数据库中

python - Google Cloud Run - 如何设置 'Access-Control-Allow-Origin' header ?

python - 在 Pandas Dataframe 中展平列表的更快方法

python - django api 支持使用 Many=True 更新序列化器,仅创建

java - apache tomcat deployer 和 core 版本有什么区别?

php - 如何使用 .htaccess/Apache2 在子目录中配置 wordpress 应用程序和简单的 php 应用程序?