RuntimeError: Working outside of request context. with gunicorn
RuntimeError: Working outside of request context. with gunicorn
每当我 运行 我的代码作为 python3 myapp.py
它工作正常但每当我使用 gunicorn -w 4 myapp:index -b 10.91.1.230:5055 &
它抛出
ion@aurora:~/TNQ$ [2019-02-05 14:26:34 +0530] [27107] [INFO] Starting
gunicorn 19.9.0
..............
27116
[2019-02-05 14:26:38 +0530] [27113] [ERROR] Error handling request /
Traceback (most recent call last):
File "/home/ion/.local/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 135, in handle
self.handle_request(listener, req, client, addr)
File "/home/ion/.local/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/home/ion/TNQ/myapp.py", line 16, in index
f = request.files['file']
File "/home/ion/.local/lib/python3.6/site-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "/home/ion/.local/lib/python3.6/site-packages/werkzeug/local.py", line 306, in _get_current_object
return self.__local()
File "/home/ion/.local/lib/python3.6/site-packages/flask/globals.py", line 37, in _lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
myapp.py
from flask import Flask,request
from CXE.src.models import class_classic as cc
app = Flask(__name__)
@app.route('/', methods=['POST'])
#def index(environ, start_response):
def index(environ, start_response):
with app.app_context():
#if request.method == 'POST':
f = request.files['file']
a = cc.initiate(f)
return a
if __name__ == '__main__':
app.run(host = '0.0.0.0',port=5505,debug=True)
我需要将代码放在 gunicorn 上以便在线程上提供它。知道为什么它不起作用吗?
它不起作用,因为 index
不是 wsgi app - 函数具有正确的签名是不够的。请改为执行以下操作:
gunicorn -w 4 myapp:app -b 10.91.1.230:5055 &
当您 运行 python3 myapp.py
时您看不到任何问题,因为 flask 的开发服务器 app.run
不需要 wsgi 应用程序,而 gunicorn 需要。话虽如此,您不妨继续并删除 index
签名。
每当我 运行 我的代码作为 python3 myapp.py
它工作正常但每当我使用 gunicorn -w 4 myapp:index -b 10.91.1.230:5055 &
它抛出
ion@aurora:~/TNQ$ [2019-02-05 14:26:34 +0530] [27107] [INFO] Starting
gunicorn 19.9.0
..............
27116
[2019-02-05 14:26:38 +0530] [27113] [ERROR] Error handling request /
Traceback (most recent call last):
File "/home/ion/.local/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 135, in handle
self.handle_request(listener, req, client, addr)
File "/home/ion/.local/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
respiter = self.wsgi(environ, resp.start_response)
File "/home/ion/TNQ/myapp.py", line 16, in index
f = request.files['file']
File "/home/ion/.local/lib/python3.6/site-packages/werkzeug/local.py", line 347, in __getattr__
return getattr(self._get_current_object(), name)
File "/home/ion/.local/lib/python3.6/site-packages/werkzeug/local.py", line 306, in _get_current_object
return self.__local()
File "/home/ion/.local/lib/python3.6/site-packages/flask/globals.py", line 37, in _lookup_req_object
raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
myapp.py
from flask import Flask,request
from CXE.src.models import class_classic as cc
app = Flask(__name__)
@app.route('/', methods=['POST'])
#def index(environ, start_response):
def index(environ, start_response):
with app.app_context():
#if request.method == 'POST':
f = request.files['file']
a = cc.initiate(f)
return a
if __name__ == '__main__':
app.run(host = '0.0.0.0',port=5505,debug=True)
我需要将代码放在 gunicorn 上以便在线程上提供它。知道为什么它不起作用吗?
它不起作用,因为 index
不是 wsgi app - 函数具有正确的签名是不够的。请改为执行以下操作:
gunicorn -w 4 myapp:app -b 10.91.1.230:5055 &
当您 运行 python3 myapp.py
时您看不到任何问题,因为 flask 的开发服务器 app.run
不需要 wsgi 应用程序,而 gunicorn 需要。话虽如此,您不妨继续并删除 index
签名。