本文整理汇总了Python中flask.copy_current_request_context方法的典型用法代码示例。如果您正苦于以下问题:Python flask.copy_current_request_context方法的具体用法?Python flask.copy_current_request_context怎么用?Python flask.copy_current_request_context使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask
的用法示例。
在下文中一共展示了flask.copy_current_request_context方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_greenlet_context_copying_api
# 需要导入模块: import flask [as 别名]
# 或者: from flask import copy_current_request_context [as 别名]
def test_greenlet_context_copying_api(self):
app = flask.Flask(__name__)
greenlets = []
@app.route('/')
def index():
reqctx = flask._request_ctx_stack.top.copy()
@flask.copy_current_request_context
def g():
self.assert_true(flask.request)
self.assert_equal(flask.current_app, app)
self.assert_equal(flask.request.path, '/')
self.assert_equal(flask.request.args['foo'], 'bar')
return 42
greenlets.append(greenlet(g))
return 'Hello World!'
rv = app.test_client().get('/?foo=bar')
self.assert_equal(rv.data, b'Hello World!')
result = greenlets[0].run()
self.assert_equal(result, 42)
# Disable test if we don't have greenlets available
示例2: copy_current_request_context
# 需要导入模块: import flask [as 别名]
# 或者: from flask import copy_current_request_context [as 别名]
def copy_current_request_context(f):
"""A helper function that decorates a function to retain the current
request context. This is useful when working with greenlets. The moment
the function is decorated a copy of the request context is created and
then pushed when the function is called.
Example::
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'
.. versionadded:: 0.10
"""
top = _request_ctx_stack.top
if top is None:
raise RuntimeError('This decorator can only be used at local scopes '
'when a request context is on the stack. For instance within '
'view functions.')
reqctx = top.copy()
def wrapper(*args, **kwargs):
with reqctx:
return f(*args, **kwargs)
return update_wrapper(wrapper, f)
示例3: _prepare_fn
# 需要导入模块: import flask [as 别名]
# 或者: from flask import copy_current_request_context [as 别名]
def _prepare_fn(self, fn, force_copy=False):
if isinstance(self._self, concurrent.futures.ThreadPoolExecutor) \
or force_copy:
fn = copy_current_request_context(fn)
fn = copy_current_app_context(fn)
return fn