从 greenlet 中访问烧瓶应用程序上下文



我有一个烧瓶脚本命令,它会产生一长串绿意。问题是,这些绿灯无法访问我的应用程序上下文。我得到一个">始终失败并出现运行时错误"(访问 app.logger,每个示例)。建议?

关于我的尝试: Spawn(method, app, arg1, arg2)

def spawn(app, arg1, arg2):
    with app.app_context():
        app.logger.debug('bla bla') # doesn't work
        ... do stuff

编辑:下面允许您访问request对象,但不能访问current_app,可能不是您要搜索的内容。

您可能正在寻找此处记录flask.copy_current_request_context(f):http://flask.pocoo.org/docs/0.10/api/#flask.copy_current_request_context

例:

import gevent
from flask import copy_current_request_context
@app.route('/')
def index():
    @copy_current_request_context
    def do_some_work():
        # do some work here, it can access flask.request like you
        # would otherwise in the view function.
        ...
    gevent.spawn(do_some_work)
    return 'Regular response'

您可以从请求中传递相关信息的副本,例如

import gevent
@app.route('/')
def index():
    def do_some_work(data):
        # do some work here with data
        ...
    data = request.get_json()
    gevent.spawn(do_some_work, data)
    return 'Regular response'

最新更新