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


Python gen.moment方法代码示例

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


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

示例1: test_moment

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_moment(self):
        calls = []

        @gen.coroutine
        def f(name, yieldable):
            for i in range(5):
                calls.append(name)
                yield yieldable
        # First, confirm the behavior without moment: each coroutine
        # monopolizes the event loop until it finishes.
        immediate = Future()
        immediate.set_result(None)
        yield [f('a', immediate), f('b', immediate)]
        self.assertEqual(''.join(calls), 'aaaaabbbbb')

        # With moment, they take turns.
        calls = []
        yield [f('a', gen.moment), f('b', gen.moment)]
        self.assertEqual(''.join(calls), 'ababababab')
        self.finished = True

        calls = []
        yield [f('a', gen.moment), f('b', immediate)]
        self.assertEqual(''.join(calls), 'abbbbbaaaa') 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:26,代码来源:gen_test.py

示例2: test_async_await

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_async_await(self):
        @gen.coroutine
        def f1():
            yield gen.moment
            raise gen.Return(42)

        # This test verifies that an async function can await a
        # yield-based gen.coroutine, and that a gen.coroutine
        # (the test method itself) can yield an async function.
        async def f2():
            result = await f1()
            return result

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

示例3: test_async_await_mixed_multi_native_future

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_async_await_mixed_multi_native_future(self):
        @gen.coroutine
        def f1():
            yield gen.moment

        async def f2():
            await f1()
            return 42

        @gen.coroutine
        def f3():
            yield gen.moment
            raise gen.Return(43)

        results = yield [f2(), f3()]
        self.assertEqual(results, [42, 43])
        self.finished = True 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:19,代码来源:gen_test.py

示例4: test_moment

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_moment(self):
        calls = []

        @gen.coroutine
        def f(name, yieldable):
            for i in range(5):
                calls.append(name)
                yield yieldable

        # First, confirm the behavior without moment: each coroutine
        # monopolizes the event loop until it finishes.
        immediate = Future()  # type: Future[None]
        immediate.set_result(None)
        yield [f("a", immediate), f("b", immediate)]
        self.assertEqual("".join(calls), "aaaaabbbbb")

        # With moment, they take turns.
        calls = []
        yield [f("a", gen.moment), f("b", gen.moment)]
        self.assertEqual("".join(calls), "ababababab")
        self.finished = True

        calls = []
        yield [f("a", gen.moment), f("b", immediate)]
        self.assertEqual("".join(calls), "abbbbbaaaa") 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:27,代码来源:gen_test.py

示例5: test_asyncio_future_debug_info

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_asyncio_future_debug_info(self):
        self.finished = True
        # Enable debug mode
        asyncio_loop = asyncio.get_event_loop()
        self.addCleanup(asyncio_loop.set_debug, asyncio_loop.get_debug())
        asyncio_loop.set_debug(True)

        def f():
            yield gen.moment

        coro = gen.coroutine(f)()
        self.assertIsInstance(coro, asyncio.Future)
        # We expect the coroutine repr() to show the place where
        # it was instantiated
        expected = "created at %s:%d" % (__file__, f.__code__.co_firstlineno + 3)
        actual = repr(coro)
        self.assertIn(expected, actual) 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:19,代码来源:gen_test.py

示例6: test_async_await

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_async_await(self):
        @gen.coroutine
        def f1():
            yield gen.moment
            raise gen.Return(42)

        # This test verifies that an async function can await a
        # yield-based gen.coroutine, and that a gen.coroutine
        # (the test method itself) can yield an async function.
        namespace = exec_test(globals(), locals(), """
        async def f2():
            result = await f1()
            return result
        """)
        result = yield namespace['f2']()
        self.assertEqual(result, 42)
        self.finished = True 
开发者ID:tp4a,项目名称:teleport,代码行数:19,代码来源:gen_test.py

示例7: test_async_await_mixed_multi_native_future

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_async_await_mixed_multi_native_future(self):
        @gen.coroutine
        def f1():
            yield gen.moment

        namespace = exec_test(globals(), locals(), """
        async def f2():
            await f1()
            return 42
        """)

        @gen.coroutine
        def f3():
            yield gen.moment
            raise gen.Return(43)

        results = yield [namespace['f2'](), f3()]
        self.assertEqual(results, [42, 43])
        self.finished = True 
开发者ID:tp4a,项目名称:teleport,代码行数:21,代码来源:gen_test.py

示例8: test_asyncio_future_debug_info

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_asyncio_future_debug_info(self):
        self.finished = True
        # Enable debug mode
        asyncio_loop = asyncio.get_event_loop()
        self.addCleanup(asyncio_loop.set_debug, asyncio_loop.get_debug())
        asyncio_loop.set_debug(True)

        def f():
            yield gen.moment

        coro = gen.coroutine(f)()
        self.assertIsInstance(coro, asyncio.Future)
        # We expect the coroutine repr() to show the place where
        # it was instantiated
        expected = ("created at %s:%d"
                    % (__file__, f.__code__.co_firstlineno + 3))
        actual = repr(coro)
        self.assertIn(expected, actual) 
开发者ID:tp4a,项目名称:teleport,代码行数:20,代码来源:gen_test.py

示例9: test_future_traceback

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_future_traceback(self):
        @gen.coroutine
        def f():
            yield gen.moment
            try:
                1 / 0
            except ZeroDivisionError:
                self.expected_frame = traceback.extract_tb(
                    sys.exc_info()[2], limit=1)[0]
                raise
        try:
            yield f()
            self.fail("didn't get expected exception")
        except ZeroDivisionError:
            tb = traceback.extract_tb(sys.exc_info()[2])
            self.assertIn(self.expected_frame, tb) 
开发者ID:tp4a,项目名称:teleport,代码行数:18,代码来源:concurrent_test.py

示例10: _server_request_loop

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def _server_request_loop(self, delegate):
        try:
            while True:
                conn = HTTP1Connection(self.stream, False,
                                       self.params, self.context)
                request_delegate = delegate.start_request(self, conn)
                try:
                    ret = yield conn.read_response(request_delegate)
                except (iostream.StreamClosedError,
                        iostream.UnsatisfiableReadError):
                    return
                except _QuietException:
                    # This exception was already logged.
                    conn.close()
                    return
                except Exception:
                    gen_log.error("Uncaught exception", exc_info=True)
                    conn.close()
                    return
                if not ret:
                    return
                yield gen.moment
        finally:
            delegate.on_close(self) 
开发者ID:tp4a,项目名称:teleport,代码行数:26,代码来源:http1connection.py

示例11: test_handle_stream_coroutine_logging

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_handle_stream_coroutine_logging(self):
        # handle_stream may be a coroutine and any exception in its
        # Future will be logged.
        class TestServer(TCPServer):
            @gen.coroutine
            def handle_stream(self, stream, address):
                yield gen.moment
                stream.close()
                1 / 0

        server = client = None
        try:
            sock, port = bind_unused_port()
            with NullContext():
                server = TestServer()
                server.add_socket(sock)
            client = IOStream(socket.socket())
            with ExpectLog(app_log, "Exception in callback"):
                yield client.connect(('localhost', port))
                yield client.read_until_close()
                yield gen.moment
        finally:
            if server is not None:
                server.stop()
            if client is not None:
                client.close() 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:28,代码来源:tcpserver_test.py

示例12: async_exception

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def async_exception(self, e):
        yield gen.moment
        raise e 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:5,代码来源:gen_test.py

示例13: test_nonblocking_put_with_getters

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_nonblocking_put_with_getters(self):
        q = queues.Queue()
        get0 = q.get()
        get1 = q.get()
        q.put_nowait(0)
        # put_nowait does *not* immediately unblock getters.
        yield gen.moment
        self.assertEqual(0, (yield get0))
        q.put_nowait(1)
        yield gen.moment
        self.assertEqual(1, (yield get1)) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:13,代码来源:queues_test.py

示例14: test_task_done_delay

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_task_done_delay(self):
        # Verify it is task_done(), not get(), that unblocks join().
        q = self.queue_class()
        q.put_nowait(0)
        join = q.join()
        self.assertFalse(join.done())
        yield q.get()
        self.assertFalse(join.done())
        yield gen.moment
        self.assertFalse(join.done())
        q.task_done()
        self.assertTrue(join.done()) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:14,代码来源:queues_test.py

示例15: test_handle_stream_coroutine_logging

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import moment [as 别名]
def test_handle_stream_coroutine_logging(self):
        # handle_stream may be a coroutine and any exception in its
        # Future will be logged.
        class TestServer(TCPServer):
            @gen.coroutine
            def handle_stream(self, stream, address):
                yield stream.read_bytes(len(b"hello"))
                stream.close()
                1 / 0

        server = client = None
        try:
            sock, port = bind_unused_port()
            server = TestServer()
            server.add_socket(sock)
            client = IOStream(socket.socket())
            with ExpectLog(app_log, "Exception in callback"):
                yield client.connect(("localhost", port))
                yield client.write(b"hello")
                yield client.read_until_close()
                yield gen.moment
        finally:
            if server is not None:
                server.stop()
            if client is not None:
                client.close() 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:28,代码来源:tcpserver_test.py


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