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


Python gen.sleep方法代码示例

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


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

示例1: test_streaming_until_close_future

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_streaming_until_close_future(self):
        server, client = self.make_iostream_pair()
        try:
            chunks = []

            @gen.coroutine
            def client_task():
                yield client.read_until_close(streaming_callback=chunks.append)

            @gen.coroutine
            def server_task():
                yield server.write(b"1234")
                yield gen.sleep(0.01)
                yield server.write(b"5678")
                server.close()

            @gen.coroutine
            def f():
                yield [client_task(), server_task()]
            self.io_loop.run_sync(f)
            self.assertEqual(chunks, [b"1234", b"5678"])
        finally:
            server.close()
            client.close() 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:26,代码来源:iostream_test.py

示例2: test_notify_n_with_timeout

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_notify_n_with_timeout(self):
        # Register callbacks 0, 1, 2, and 3. Callback 1 has a timeout.
        # Wait for that timeout to expire, then do notify(2) and make
        # sure everyone runs. Verifies that a timed-out callback does
        # not count against the 'n' argument to notify().
        c = locks.Condition()
        self.record_done(c.wait(), 0)
        self.record_done(c.wait(timedelta(seconds=0.01)), 1)
        self.record_done(c.wait(), 2)
        self.record_done(c.wait(), 3)

        # Wait for callback 1 to time out.
        yield gen.sleep(0.02)
        self.assertEqual(['timeout'], self.history)

        c.notify(2)
        yield gen.sleep(0.01)
        self.assertEqual(['timeout', 0, 2], self.history)
        self.assertEqual(['timeout', 0, 2], self.history)
        c.notify()
        self.assertEqual(['timeout', 0, 2, 3], self.history) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:23,代码来源:locks_test.py

示例3: test_garbage_collection

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_garbage_collection(self):
        # Test that timed-out waiters are occasionally cleaned from the queue.
        c = locks.Condition()
        for _ in range(101):
            c.wait(timedelta(seconds=0.01))

        future = c.wait()
        self.assertEqual(102, len(c._waiters))

        # Let first 101 waiters time out, triggering a collection.
        yield gen.sleep(0.02)
        self.assertEqual(1, len(c._waiters))

        # Final waiter is still active.
        self.assertFalse(future.done())
        c.notify()
        self.assertTrue(future.done()) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:19,代码来源:locks_test.py

示例4: test_context_manager_contended

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_context_manager_contended(self):
        sem = locks.Semaphore()
        history = []

        @gen.coroutine
        def f(index):
            with (yield sem.acquire()):
                history.append('acquired %d' % index)
                yield gen.sleep(0.01)
                history.append('release %d' % index)

        yield [f(i) for i in range(2)]

        expected_history = []
        for i in range(2):
            expected_history.extend(['acquired %d' % i, 'release %d' % i])

        self.assertEqual(expected_history, history) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:20,代码来源:locks_test.py

示例5: test_task_done

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_task_done(self):
        q = self.queue_class()
        for i in range(100):
            q.put_nowait(i)

        self.accumulator = 0

        @gen.coroutine
        def worker():
            while True:
                item = yield q.get()
                self.accumulator += item
                q.task_done()
                yield gen.sleep(random() * 0.01)

        # Two coroutines share work.
        worker()
        worker()
        yield q.join()
        self.assertEqual(sum(range(100)), self.accumulator) 
开发者ID:tao12345666333,项目名称:tornado-zh,代码行数:22,代码来源:queues_test.py

示例6: main

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def main():
    try:
        # Open serial port
        ser = serial.Serial('/dev/ttys009', 9600)

        # Create XBee Series 1 object
        xbee = XBee(ser, callback=handle_data)

        while True:
            yield gen.sleep(1)
            # Send AT packet
            xbee.send('at', frame_id='B', command='DL')
    except KeyboardInterrupt:
        ioloop.IOLoop.current().stop()
    finally:
        xbee.halt()
        ser.close() 
开发者ID:niolabs,项目名称:python-xbee,代码行数:19,代码来源:tornado_example.py

示例7: __init__

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def __init__(
        self, async_client_class: Type["AsyncHTTPClient"] = None, **kwargs: Any
    ) -> None:
        # Initialize self._closed at the beginning of the constructor
        # so that an exception raised here doesn't lead to confusing
        # failures in __del__.
        self._closed = True
        self._io_loop = IOLoop(make_current=False)
        if async_client_class is None:
            async_client_class = AsyncHTTPClient

        # Create the client while our IOLoop is "current", without
        # clobbering the thread's real current IOLoop (if any).
        async def make_client() -> "AsyncHTTPClient":
            await gen.sleep(0)
            assert async_client_class is not None
            return async_client_class(**kwargs)

        self._async_client = self._io_loop.run_sync(make_client)
        self._closed = False 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:22,代码来源:httpclient.py

示例8: test_close_buffered_data

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_close_buffered_data(self):
        # Similar to the previous test, but with data stored in the OS's
        # socket buffers instead of the IOStream's read buffer.  Out-of-band
        # close notifications must be delayed until all data has been
        # drained into the IOStream buffer. (epoll used to use out-of-band
        # close events with EPOLLRDHUP, but no longer)
        #
        # This depends on the read_chunk_size being smaller than the
        # OS socket buffer, so make it small.
        rs, ws = yield self.make_iostream_pair(read_chunk_size=256)
        try:
            ws.write(b"A" * 512)
            data = yield rs.read_bytes(256)
            self.assertEqual(b"A" * 256, data)
            ws.close()
            # Allow the close to propagate to the `rs` side of the
            # connection.  Using add_callback instead of add_timeout
            # doesn't seem to work, even with multiple iterations
            yield gen.sleep(0.01)
            data = yield rs.read_bytes(256)
            self.assertEqual(b"A" * 256, data)
        finally:
            ws.close()
            rs.close() 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:26,代码来源:iostream_test.py

示例9: test_flow_control

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_flow_control(self):
        MB = 1024 * 1024
        rs, ws = yield self.make_iostream_pair(max_buffer_size=5 * MB)
        try:
            # Client writes more than the rs will accept.
            ws.write(b"a" * 10 * MB)
            # The rs pauses while reading.
            yield rs.read_bytes(MB)
            yield gen.sleep(0.1)
            # The ws's writes have been blocked; the rs can
            # continue to read gradually.
            for i in range(9):
                yield rs.read_bytes(MB)
        finally:
            rs.close()
            ws.close() 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:18,代码来源:iostream_test.py

示例10: test_singleton

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_singleton(self):
        # Class "constructor" reuses objects on the same IOLoop
        self.assertTrue(SimpleAsyncHTTPClient() is SimpleAsyncHTTPClient())
        # unless force_instance is used
        self.assertTrue(
            SimpleAsyncHTTPClient() is not SimpleAsyncHTTPClient(force_instance=True)
        )
        # different IOLoops use different objects
        with closing(IOLoop()) as io_loop2:

            async def make_client():
                await gen.sleep(0)
                return SimpleAsyncHTTPClient()

            client1 = self.io_loop.run_sync(make_client)
            client2 = io_loop2.run_sync(make_client)
            self.assertTrue(client1 is not client2) 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:19,代码来源:simple_httpclient_test.py

示例11: test_native_body_producer_content_length

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_native_body_producer_content_length(self):
        async def body_producer(write):
            await write(b"1234")
            import asyncio

            await asyncio.sleep(0)
            await write(b"5678")

        response = self.fetch(
            "/echo_post",
            method="POST",
            body_producer=body_producer,
            headers={"Content-Length": "8"},
        )
        response.rethrow()
        self.assertEqual(response.body, b"12345678") 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:18,代码来源:simple_httpclient_test.py

示例12: test_notify_n_with_timeout

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_notify_n_with_timeout(self):
        # Register callbacks 0, 1, 2, and 3. Callback 1 has a timeout.
        # Wait for that timeout to expire, then do notify(2) and make
        # sure everyone runs. Verifies that a timed-out callback does
        # not count against the 'n' argument to notify().
        c = locks.Condition()
        self.record_done(c.wait(), 0)
        self.record_done(c.wait(timedelta(seconds=0.01)), 1)
        self.record_done(c.wait(), 2)
        self.record_done(c.wait(), 3)

        # Wait for callback 1 to time out.
        yield gen.sleep(0.02)
        self.assertEqual(["timeout"], self.history)

        c.notify(2)
        yield gen.sleep(0.01)
        self.assertEqual(["timeout", 0, 2], self.history)
        self.assertEqual(["timeout", 0, 2], self.history)
        c.notify()
        yield
        self.assertEqual(["timeout", 0, 2, 3], self.history) 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:24,代码来源:locks_test.py

示例13: test_garbage_collection

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_garbage_collection(self):
        # Test that timed-out waiters are occasionally cleaned from the queue.
        c = locks.Condition()
        for _ in range(101):
            c.wait(timedelta(seconds=0.01))

        future = asyncio.ensure_future(c.wait())
        self.assertEqual(102, len(c._waiters))

        # Let first 101 waiters time out, triggering a collection.
        yield gen.sleep(0.02)
        self.assertEqual(1, len(c._waiters))

        # Final waiter is still active.
        self.assertFalse(future.done())
        c.notify()
        self.assertTrue(future.done()) 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:19,代码来源:locks_test.py

示例14: test_context_manager_contended

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_context_manager_contended(self):
        sem = locks.Semaphore()
        history = []

        @gen.coroutine
        def f(index):
            with (yield sem.acquire()):
                history.append("acquired %d" % index)
                yield gen.sleep(0.01)
                history.append("release %d" % index)

        yield [f(i) for i in range(2)]

        expected_history = []
        for i in range(2):
            expected_history.extend(["acquired %d" % i, "release %d" % i])

        self.assertEqual(expected_history, history) 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:20,代码来源:locks_test.py

示例15: test_timeout

# 需要导入模块: from tornado import gen [as 别名]
# 或者: from tornado.gen import sleep [as 别名]
def test_timeout(self):
        # Set a short timeout and exceed it.
        @gen_test(timeout=0.1)
        def test(self):
            yield gen.sleep(1)

        # This can't use assertRaises because we need to inspect the
        # exc_info triple (and not just the exception object)
        try:
            test(self)
            self.fail("did not get expected exception")
        except ioloop.TimeoutError:
            # The stack trace should blame the add_timeout line, not just
            # unrelated IOLoop/testing internals.
            self.assertIn("gen.sleep(1)", traceback.format_exc())

        self.finished = True 
开发者ID:opendevops-cn,项目名称:opendevops,代码行数:19,代码来源:testing_test.py


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