2

我有一个烧瓶脚本命令,它产生一长串绿色小菜。问题是,这些 greenlets 无法访问我的应用程序上下文。我总是得到一个“> failed with RuntimeError”(访问 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
4

2 回答 2

1

编辑:下面使您可以访问该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'
于 2015-03-11T13:37:51.453 回答
0

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

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'
于 2015-05-08T21:39:35.587 回答