當前位置: 首頁>>代碼示例>>Python>>正文


Python stack_context.StackContext方法代碼示例

本文整理匯總了Python中tornado.stack_context.StackContext方法的典型用法代碼示例。如果您正苦於以下問題:Python stack_context.StackContext方法的具體用法?Python stack_context.StackContext怎麽用?Python stack_context.StackContext使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tornado.stack_context的用法示例。


在下文中一共展示了stack_context.StackContext方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_pre_wrap_with_args

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def test_pre_wrap_with_args(self):
        # Same as test_pre_wrap, but the function takes arguments.
        # Implementation note: The function must not be wrapped in a
        # functools.partial until after it has been passed through
        # stack_context.wrap
        def f1(foo, bar):
            self.assertIn('c1', self.active_contexts)
            self.assertNotIn('c2', self.active_contexts)
            self.stop((foo, bar))

        with StackContext(functools.partial(self.context, 'c1')):
            wrapped = wrap(f1)

        with StackContext(functools.partial(self.context, 'c2')):
            self.add_callback(wrapped, 1, bar=2)

        result = self.wait()
        self.assertEqual(result, (1, 2)) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:20,代碼來源:ioloop_test.py

示例2: test_pre_wrap

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def test_pre_wrap(self):
        # A pre-wrapped callback is run in the context in which it was
        # wrapped, not when it was added to the IOLoop.
        def f1():
            self.assertIn('c1', self.active_contexts)
            self.assertNotIn('c2', self.active_contexts)
            self.stop()

        with ignore_deprecation():
            with StackContext(functools.partial(self.context, 'c1')):
                wrapped = wrap(f1)

            with StackContext(functools.partial(self.context, 'c2')):
                self.add_callback(wrapped)

        self.wait() 
開發者ID:tp4a,項目名稱:teleport,代碼行數:18,代碼來源:ioloop_test.py

示例3: test_pre_wrap_with_args

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def test_pre_wrap_with_args(self):
        # Same as test_pre_wrap, but the function takes arguments.
        # Implementation note: The function must not be wrapped in a
        # functools.partial until after it has been passed through
        # stack_context.wrap
        def f1(foo, bar):
            self.assertIn('c1', self.active_contexts)
            self.assertNotIn('c2', self.active_contexts)
            self.stop((foo, bar))

        with ignore_deprecation():
            with StackContext(functools.partial(self.context, 'c1')):
                wrapped = wrap(f1)

            with StackContext(functools.partial(self.context, 'c2')):
                self.add_callback(wrapped, 1, bar=2)

        result = self.wait()
        self.assertEqual(result, (1, 2)) 
開發者ID:tp4a,項目名稱:teleport,代碼行數:21,代碼來源:ioloop_test.py

示例4: test_exit_library_context

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def test_exit_library_context(self):
        def library_function(callback):
            # capture the caller's context before introducing our own
            callback = wrap(callback)
            with StackContext(functools.partial(self.context, 'library')):
                self.io_loop.add_callback(
                  functools.partial(library_inner_callback, callback))

        def library_inner_callback(callback):
            self.assertEqual(self.active_contexts[-2:],
                             ['application', 'library'])
            callback()

        def final_callback():
            # implementation detail:  the full context stack at this point
            # is ['application', 'library', 'application'].  The 'library'
            # context was not removed, but is no longer innermost so
            # the application context takes precedence.
            self.assertEqual(self.active_contexts[-1], 'application')
            self.stop()
        with StackContext(functools.partial(self.context, 'application')):
            library_function(final_callback)
        self.wait() 
開發者ID:omererdem,項目名稱:honeything,代碼行數:25,代碼來源:stack_context_test.py

示例5: test_request_context_manager_backwards_compatible

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def test_request_context_manager_backwards_compatible(self):
        span = opentracing.tracer.start_span(operation_name='test')

        @gen.coroutine
        def check():
            assert get_current_span() == span

        # Bypass ScopeManager/span_in_stack_context() and use
        # RequestContextManager directly.
        def run_coroutine(span, coro):
            def mgr():
                return RequestContextManager(span)

            with stack_context.StackContext(mgr):
                return coro()

        yield run_coroutine(span, check) 
開發者ID:uber-common,項目名稱:opentracing-python-instrumentation,代碼行數:19,代碼來源:test_tornado_request_context.py

示例6: make_context

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def make_context(self):
        return stack_context.StackContext(self.__context) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:4,代碼來源:stack_context_benchmark.py

示例7: function_with_stack_context

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def function_with_stack_context(self, callback):
        # Technically this function should stack_context.wrap its callback
        # upon entry.  However, it is very common for this step to be
        # omitted.
        def step2():
            self.assertEqual(self.named_contexts, ['a'])
            self.io_loop.add_callback(callback)

        with stack_context.StackContext(self.named_context('a')):
            self.io_loop.add_callback(step2) 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:12,代碼來源:gen_test.py

示例8: test_handle_callback_exception

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def test_handle_callback_exception(self):
        # IOLoop.handle_callback_exception can be overridden to catch
        # exceptions in callbacks.
        def handle_callback_exception(callback):
            self.assertIs(sys.exc_info()[0], ZeroDivisionError)
            self.stop()
        self.io_loop.handle_callback_exception = handle_callback_exception
        with NullContext():
            # remove the test StackContext that would see this uncaught
            # exception as a test failure.
            self.io_loop.add_callback(lambda: 1 / 0)
        self.wait() 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:14,代碼來源:ioloop_test.py

示例9: test_pre_wrap

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def test_pre_wrap(self):
        # A pre-wrapped callback is run in the context in which it was
        # wrapped, not when it was added to the IOLoop.
        def f1():
            self.assertIn('c1', self.active_contexts)
            self.assertNotIn('c2', self.active_contexts)
            self.stop()

        with StackContext(functools.partial(self.context, 'c1')):
            wrapped = wrap(f1)

        with StackContext(functools.partial(self.context, 'c2')):
            self.add_callback(wrapped)

        self.wait() 
開發者ID:tao12345666333,項目名稱:tornado-zh,代碼行數:17,代碼來源:ioloop_test.py

示例10: _execute

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def _execute(self, transforms, *args, **kwargs):
        current_context = {'request': self.request}

        with stack_context.StackContext(functools.partial(ThreadlocalLikeRequestContext, **current_context)):
            return super(_HandlerPatch, self)._execute(transforms, *args, **kwargs) 
開發者ID:mqingyn,項目名稱:torngas,代碼行數:7,代碼來源:handler.py

示例11: run

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def run(self, result=None):
        with StackContext(self._stack_context):
            super(AsyncTestCase, self).run(result)
        # In case an exception escaped super.run or the StackContext caught
        # an exception when there wasn't a wait() to re-raise it, do so here.
        self.__rethrow() 
開發者ID:omererdem,項目名稱:honeything,代碼行數:8,代碼來源:testing.py

示例12: wait

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def wait(self, condition=None, timeout=5):
        """Runs the IOLoop until stop is called or timeout has passed.

        In the event of a timeout, an exception will be thrown.

        If condition is not None, the IOLoop will be restarted after stop()
        until condition() returns true.
        """
        if not self.__stopped:
            if timeout:
                def timeout_func():
                    try:
                        raise self.failureException(
                          'Async operation timed out after %s seconds' %
                          timeout)
                    except Exception:
                        self.__failure = sys.exc_info()
                    self.stop()
                if self.__timeout is not None:
                    self.io_loop.remove_timeout(self.__timeout)
                self.__timeout = self.io_loop.add_timeout(datetime.timedelta(seconds=timeout), timeout_func)
            while True:
                self.__running = True
                with NullContext():
                    # Wipe out the StackContext that was established in
                    # self.run() so that all callbacks executed inside the
                    # IOLoop will re-run it.
                    self.io_loop.start()
                if (self.__failure is not None or
                    condition is None or condition()):
                    break
        assert self.__stopped
        self.__stopped = False
        self.__rethrow()
        result = self.__stop_args
        self.__stop_args = None
        return result 
開發者ID:omererdem,項目名稱:honeything,代碼行數:39,代碼來源:testing.py

示例13: context

# 需要導入模塊: from tornado import stack_context [as 別名]
# 或者: from tornado.stack_context import StackContext [as 別名]
def context(self, name):
        self.active_contexts.append(name)
        yield
        self.assertEqual(self.active_contexts.pop(), name)

    # Simulates the effect of an asynchronous library that uses its own
    # StackContext internally and then returns control to the application. 
開發者ID:omererdem,項目名稱:honeything,代碼行數:9,代碼來源:stack_context_test.py


注:本文中的tornado.stack_context.StackContext方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。