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