當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。