UWSGI生产中的Pytorch模型预测

时间:2019-05-12 02:11:00

标签: nginx flask pytorch uwsgi

我在生产中部署pytorch模型时遇到问题。为了进行演示,我构建了一个简单的模型和一个flask应用程序。我将所有内容放入docker容器(pytorch + flask + uwsgi),再加上另一个用于nginx的容器。一切运行良好,我的应用程序已渲染,可以在其中导航。但是,我导航到启动模型预测的URL,服务器挂起并且似乎不计算任何内容。

uWSGI的运行方式如下:

/opt/conda/bin/uwsgi --ini  /usr/src/web/uwsgi.ini

与uwsgi.ini

[uwsgi]
#application's base folder
chdir = /usr/src/web/
#python module to import
wsgi-file = /usr/src/web/wsgi.py
callable = app
#socket file's location
socket = /usr/src/web/uwsgi.sock
#permissions for the socket file
chmod-socket = 666
# Port to expose
http = :5000
# Cleanup the socket when process stops
vacuum = true
#Log directory
logto = /usr/src/web/app.log
# minimum number of workers to keep at all times
cheaper = 2
processes = 16

如前所述,服务器挂起,我终于超时了。奇怪的是,我使用

直接运行flask应用程序(也在容器中)
python /usr/src/web/manage.py runserver --host 0.0.0.0 

我很快就能得到我的预测

1 个答案:

答案 0 :(得分:0)

我认为这与 https://discuss.pytorch.org/t/basic-operations-do-not-work-in-1-1-0-with-uwsgi-flask/50257
也许可以像上面提到的那样尝试:

app = flask.Flask(__name__)
segmentator = None

@app.before_first_request
def load_segmentator():
    global segmentator
    segmentator = Segmentator()

其中Segmentator是带有pytorch的{​​{1}}的类,该类将权重加载到nn.Module

仅供参考,此解决方案仅适用于一个应用程序,而不适用于另一个应用程序