在 greenlet 中访问 flask.g

Access flask.g inside greenlet

我正在使用 Flask + gevent 并希望在 greenlet 的目标函数内访问 flask.g 应用程序全局。 我正在使用 copy_current_request_context 装饰器,情况与文档中给出的示例非常相似:

import gevent
from flask import copy_current_request_context, g

@app.route('/')
def index():
    g.user_data = 'foobar'
    g.more_user_data = 'baz'

    @copy_current_request_context
    def do_some_work():
        some_func(g.user_data, g.more_user_data)
        ...  

    gevent.spawn(do_some_work)
    return 'Regular response'

但是,我收到以下错误:

AttributeError: '_AppCtxGlobals' object has no attribute 'user_data'

我认为复制请求上下文时会推送一个新的应用程序上下文?我在 Flask 代码中设置了跟踪 here and that seems to be the case. So the error isn't all that surprising because the flask.g object is application context scoped as of 0.10 (see http://flask.pocoo.org/docs/0.12/api/#flask.Flask.app_ctx_globals_class).

显然,我可以将用户数据作为参数传递给目标函数:

import gevent
from flask import g

@app.route('/')
def index():
    g.user_data = 'foobar'
    g.more_user_data = 'baz'

    def do_some_work(user_data, more_user_data):
        some_func(user_data, more_user_data)
        ...  

    gevent.spawn(do_some_work, g.user_data, g.more_user_data)
    return 'Regular response'

这很好用,但我希望尽可能使用 flask.g

flask.gbound 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()只复制请求上下文,但给你一个新的应用上下文。您可以创建一个通过闭包传递当前应用程序上下文:

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 _

但是,我更喜欢通过参数显式地将数据传递给 greenlet,这样更干净。