我有一个flask-script命令,它会生成一长串greenlet。问题是,这些greenlet无法访问我的应用程序上下文。我得到了 ">失败了RuntimeError"在任何时候(访问app.logger,每个例子)。建议?
我的尝试: spawn(方法,app,arg1,arg2)
def spawn(app, arg1, arg2):
with app.app_context():
app.logger.debug('bla bla') # doesn't work
... do stuff
答案 0 :(得分: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'
答案 1 :(得分: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'