通过Gunicorn运行时烧瓶抛出运行时错误,但没有通过烧瓶运行

时间:2020-07-13 22:39:57

标签: python flask heroku gunicorn

当我使用Flask构建我的第一个网站的后端时,我将跟随一个视频系列作为参考。其中一个视频让我逐字编写了此函数以用作装饰器:

# add this as a decorator to pages that require a login
def login_required(f):
    @wraps(f)
    def wrap(*args, **kwargs):
        if 'logged_in' in session:
            return f(*args, **kwargs)
        else:
            flash('You need to login.')
            return redirect(url_for('login'))
    return wrap 

,用于在页面需要时重定向到登录名。使用python3 -m flask run测试应用程序时,此方法运行良好,但是运行gunicorn main:main

时,我收到以下堆栈跟踪的内部服务器错误消息
[2020-07-13 18:33:52 -0400] [6272] [INFO] Listening at: http://127.0.0.1:8000 (6272)
[2020-07-13 18:33:52 -0400] [6272] [INFO] Using worker: sync
[2020-07-13 18:33:52 -0400] [6274] [INFO] Booting worker with pid: 6274
[2020-07-13 18:33:59 -0400] [6274] [ERROR] Error handling request /
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/workers/sync.py", line 134, in handle
    self.handle_request(listener, req, client, addr)
  File "/usr/local/lib/python3.8/dist-packages/gunicorn/workers/sync.py", line 175, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/home/john/Developer/runjs/main.py", line 15, in wrap
    if 'logged_in' in session:
  File "/home/john/.local/lib/python3.8/site-packages/werkzeug/local.py", line 379, in <lambda>
    __contains__ = lambda x, i: i in x._get_current_object()
  File "/home/john/.local/lib/python3.8/site-packages/werkzeug/local.py", line 306, in _get_current_object
    return self.__local()
  File "/home/john/.local/lib/python3.8/site-packages/flask/globals.py", line 38, in _lookup_req_object
    raise RuntimeError(_request_ctx_err_msg)
RuntimeError: Working outside of request context.

我不确定函数运行Gunicorn时会导致其中断,究竟是什么问题。我曾尝试研究此问题,但建议的修复似乎都与我的具体情况无关,我对其为何在本地工作感到困惑。

0 个答案:

没有答案