Access flask.g inside greenlet

邮差的信 提交于 2019-12-04 23:46:46

flask.g is bound with the app context, not on request context, as the doc says:

Starting with Flask 0.10 this is stored on the application context and no longer on the request context ...

copy_current_request_context() only copy request context, but give you a new app context. You could create one to pass current app context with closure:

def copy_current_app_context(f):
    from flask.globals import _app_ctx_stack
    appctx = _app_ctx_stack.top
    def _(*args, **kwargs):
        with appctx:
            return f(*args, **kwargs)
    return _

However, I prefer pass data to greenlet explicitly via arguments, which is cleaner.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!