我有一个flask-script命令,可以生成一个很长的greenlet序列。问题是,这些greenlet无法访问我的应用程序上下文。我总是得到一个"> failed with RuntimeError“(访问app.logger,根据示例)。有什么建议吗?
在我的一次尝试中:派生(方法,应用程序,arg1,arg2)
def spawn(app, arg1, arg2):
with app.app_context():
app.logger.debug('bla bla') # doesn't work
... do stuff发布于 2015-03-11 21:37:51
编辑:下面提供了访问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-05-09 05:39:36
您可以从请求中传递相关信息的副本,例如
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'https://stackoverflow.com/questions/28977290
复制相似问题