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


Python gen.Callback方法代码示例

本文整理汇总了Python中tornado.gen.Callback方法的典型用法代码示例。如果您正苦于以下问题:Python gen.Callback方法的具体用法?Python gen.Callback怎么用?Python gen.Callback使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tornado.gen的用法示例。


在下文中一共展示了gen.Callback方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_async_await_mixed_multi_native_yieldpoint

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_async_await_mixed_multi_native_yieldpoint(self):
        namespace = exec_test(globals(), locals(), """
        async def f1():
            await gen.Task(self.io_loop.add_callback)
            return 42
        """)

        @gen.coroutine
        def f2():
            yield gen.Task(self.io_loop.add_callback)
            raise gen.Return(43)

        f2(callback=(yield gen.Callback('cb')))
        results = yield [namespace['f1'](), gen.Wait('cb')]
        self.assertEqual(results, [42, 43])
        self.finished = True 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:18,代码来源:gen_test.py

示例2: test_swallow_context_exception

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_swallow_context_exception(self):
        # Test exception handling: exceptions thrown into the stack context
        # can be caught and ignored.
        @gen.coroutine
        def f2():
            (yield gen.Callback(1))()
            yield gen.Wait(1)
            self.io_loop.add_callback(lambda: 1 / 0)
            try:
                yield gen.Task(self.io_loop.add_timeout,
                               self.io_loop.time() + 10)
            except ZeroDivisionError:
                raise gen.Return(42)

        result = yield f2()
        self.assertEqual(result, 42)
        self.finished = True 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:19,代码来源:gen_test.py

示例3: test_yield_in_with

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_yield_in_with(self):
        @gen.engine
        def f():
            self.callback = yield gen.Callback('a')
            with StackContext(functools.partial(self.context, 'c1')):
                # This yield is a problem: the generator will be suspended
                # and the StackContext's __exit__ is not called yet, so
                # the context will be left on _state.contexts for anything
                # that runs before the yield resolves.
                yield gen.Wait('a')

        with self.assertRaises(StackContextInconsistentError):
            f()
            self.wait()
        # Cleanup: to avoid GC warnings (which for some reason only seem
        # to show up on py33-asyncio), invoke the callback (which will do
        # nothing since the gen.Runner is already finished) and delete it.
        self.callback()
        del self.callback 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:21,代码来源:stack_context_test.py

示例4: test_replace_context_exception

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_replace_context_exception(self):
        # Test exception handling: exceptions thrown into the stack context
        # can be caught and replaced.
        # Note that this test and the following are for behavior that is
        # not really supported any more:  coroutines no longer create a
        # stack context automatically; but one is created after the first
        # YieldPoint (i.e. not a Future).
        @gen.coroutine
        def f2():
            (yield gen.Callback(1))()
            yield gen.Wait(1)
            self.io_loop.add_callback(lambda: 1 / 0)
            try:
                yield gen.Task(self.io_loop.add_timeout,
                               self.io_loop.time() + 10)
            except ZeroDivisionError:
                raise KeyError()

        future = f2()
        with self.assertRaises(KeyError):
            yield future
        self.finished = True 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:24,代码来源:gen_test.py

示例5: resolve

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def resolve(self, host, port, family=0):
        if is_valid_ip(host):
            addresses = [host]
        else:
            # gethostbyname doesn't take callback as a kwarg
            self.channel.gethostbyname(host, family, (yield gen.Callback(1)))
            callback_args = yield gen.Wait(1)
            assert isinstance(callback_args, gen.Arguments)
            assert not callback_args.kwargs
            result, error = callback_args.args
            if error:
                raise Exception('C-Ares returned error %s: %s while resolving %s' %
                                (error, pycares.errno.strerror(error), host))
            addresses = result.addresses
        addrinfo = []
        for address in addresses:
            if '.' in address:
                address_family = socket.AF_INET
            elif ':' in address:
                address_family = socket.AF_INET6
            else:
                address_family = socket.AF_UNSPEC
            if family != socket.AF_UNSPEC and family != address_family:
                raise Exception('Requested socket family %d but got %d' %
                                (family, address_family))
            addrinfo.append((address_family, (address, port)))
        raise gen.Return(addrinfo) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:29,代码来源:caresresolver.py

示例6: test_inline_cb

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_inline_cb(self):
        @gen.engine
        def f():
            (yield gen.Callback("k1"))()
            res = yield gen.Wait("k1")
            self.assertTrue(res is None)
            self.stop()
        self.run_gen(f) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:10,代码来源:gen_test.py

示例7: test_ioloop_cb

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_ioloop_cb(self):
        @gen.engine
        def f():
            self.io_loop.add_callback((yield gen.Callback("k1")))
            yield gen.Wait("k1")
            self.stop()
        self.run_gen(f) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:9,代码来源:gen_test.py

示例8: test_exception_phase2

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_exception_phase2(self):
        @gen.engine
        def f():
            self.io_loop.add_callback((yield gen.Callback("k1")))
            yield gen.Wait("k1")
            1 / 0
        self.assertRaises(ZeroDivisionError, self.run_gen, f) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:9,代码来源:gen_test.py

示例9: test_with_arg

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_with_arg(self):
        @gen.engine
        def f():
            (yield gen.Callback("k1"))(42)
            res = yield gen.Wait("k1")
            self.assertEqual(42, res)
            self.stop()
        self.run_gen(f) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:10,代码来源:gen_test.py

示例10: test_key_reuse

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_key_reuse(self):
        @gen.engine
        def f():
            yield gen.Callback("k1")
            yield gen.Callback("k1")
            self.stop()
        self.assertRaises(gen.KeyReuseError, self.run_gen, f) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:9,代码来源:gen_test.py

示例11: test_key_reuse_tuple

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_key_reuse_tuple(self):
        @gen.engine
        def f():
            yield gen.Callback((1, 2))
            yield gen.Callback((1, 2))
            self.stop()
        self.assertRaises(gen.KeyReuseError, self.run_gen, f) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:9,代码来源:gen_test.py

示例12: test_key_mismatch

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_key_mismatch(self):
        @gen.engine
        def f():
            yield gen.Callback("k1")
            yield gen.Wait("k2")
            self.stop()
        self.assertRaises(gen.UnknownKeyError, self.run_gen, f) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:9,代码来源:gen_test.py

示例13: test_key_mismatch_tuple

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_key_mismatch_tuple(self):
        @gen.engine
        def f():
            yield gen.Callback((1, 2))
            yield gen.Wait((2, 3))
            self.stop()
        self.assertRaises(gen.UnknownKeyError, self.run_gen, f) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:9,代码来源:gen_test.py

示例14: test_leaked_callback

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_leaked_callback(self):
        @gen.engine
        def f():
            yield gen.Callback("k1")
            self.stop()
        self.assertRaises(gen.LeakedCallbackError, self.run_gen, f) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:8,代码来源:gen_test.py

示例15: test_parallel_callback

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import Callback [as 别名]
def test_parallel_callback(self):
        @gen.engine
        def f():
            for k in range(3):
                self.io_loop.add_callback((yield gen.Callback(k)))
            yield gen.Wait(1)
            self.io_loop.add_callback((yield gen.Callback(3)))
            yield gen.Wait(0)
            yield gen.Wait(3)
            yield gen.Wait(2)
            self.stop()
        self.run_gen(f) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:14,代码来源:gen_test.py


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