当前位置: 首页>>代码示例>>Python>>正文


Python flask.copy_current_request_context方法代码示例

本文整理汇总了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 
开发者ID:chalasr,项目名称:Flask-P2P,代码行数:26,代码来源:reqctx.py

示例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) 
开发者ID:Frank-qlu,项目名称:recruit,代码行数:35,代码来源:ctx.py

示例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 
开发者ID:dchevell,项目名称:flask-executor,代码行数:8,代码来源:executor.py


注:本文中的flask.copy_current_request_context方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。