Python tornado.gen.sleep() Examples
The following are 30
code examples of tornado.gen.sleep().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tornado.gen
, or try the search function
.
Example #1
Source File: httpclient.py From opendevops with GNU General Public License v3.0 | 6 votes |
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
Example #2
Source File: locks_test.py From tornado-zh with MIT License | 6 votes |
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())
Example #3
Source File: locks_test.py From tornado-zh with MIT License | 6 votes |
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)
Example #4
Source File: iostream_test.py From tornado-zh with MIT License | 6 votes |
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()
Example #5
Source File: locks_test.py From tornado-zh with MIT License | 6 votes |
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)
Example #6
Source File: testing_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
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
Example #7
Source File: locks_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
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)
Example #8
Source File: queues_test.py From tornado-zh with MIT License | 6 votes |
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)
Example #9
Source File: locks_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
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())
Example #10
Source File: iostream_test.py From tornado-zh with MIT License | 6 votes |
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()
Example #11
Source File: locks_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
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)
Example #12
Source File: locks_test.py From tornado-zh with MIT License | 6 votes |
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())
Example #13
Source File: queues_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
def test_put_clears_timed_out_getters(self): q = queues.Queue() # type: queues.Queue[int] getters = [ asyncio.ensure_future(q.get(timedelta(seconds=0.01))) for _ in range(10) ] get = asyncio.ensure_future(q.get()) q.get() self.assertEqual(12, len(q._getters)) yield gen.sleep(0.02) self.assertEqual(12, len(q._getters)) self.assertFalse(get.done()) # Final waiters still active. q.put(0) # put() clears the waiters. self.assertEqual(1, len(q._getters)) self.assertEqual(0, (yield get)) for getter in getters: self.assertRaises(TimeoutError, getter.result)
Example #14
Source File: iostream_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
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()
Example #15
Source File: queues_test.py From tornado-zh with MIT License | 6 votes |
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)
Example #16
Source File: iostream_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
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()
Example #17
Source File: simple_httpclient_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
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)
Example #18
Source File: simple_httpclient_test.py From opendevops with GNU General Public License v3.0 | 6 votes |
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")
Example #19
Source File: locks_test.py From tornado-zh with MIT License | 6 votes |
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)
Example #20
Source File: locks_test.py From tornado-zh with MIT License | 6 votes |
def test_garbage_collection(self): # Test that timed-out waiters are occasionally cleaned from the queue. sem = locks.Semaphore(value=0) futures = [sem.acquire(timedelta(seconds=0.01)) for _ in range(101)] future = sem.acquire() self.assertEqual(102, len(sem._waiters)) # Let first 101 waiters time out, triggering a collection. yield gen.sleep(0.02) self.assertEqual(1, len(sem._waiters)) # Final waiter is still active. self.assertFalse(future.done()) sem.release() self.assertTrue(future.done()) # Prevent "Future exception was never retrieved" messages. for future in futures: self.assertRaises(TimeoutError, future.result)
Example #21
Source File: locks_test.py From tornado-zh with MIT License | 6 votes |
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)
Example #22
Source File: queues_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_put_clears_timed_out_putters(self): q = queues.Queue(1) # type: queues.Queue[int] # First putter succeeds, remainder block. putters = [q.put(i, timedelta(seconds=0.01)) for i in range(10)] put = q.put(10) self.assertEqual(10, len(q._putters)) yield gen.sleep(0.02) self.assertEqual(10, len(q._putters)) self.assertFalse(put.done()) # Final waiter is still active. q.put(11) # put() clears the waiters. self.assertEqual(2, len(q._putters)) for putter in putters[1:]: self.assertRaises(TimeoutError, putter.result)
Example #23
Source File: simple_httpclient_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_request_timeout(self): timeout = 0.1 if os.name == "nt": timeout = 0.5 with self.assertRaises(HTTPTimeoutError): self.fetch("/trigger?wake=false", request_timeout=timeout, raise_error=True) # trigger the hanging request to let it clean up after itself self.triggers.popleft()() self.io_loop.run_sync(lambda: gen.sleep(0))
Example #24
Source File: iostream_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_read_into(self): rs, ws = yield self.make_iostream_pair() def sleep_some(): self.io_loop.run_sync(lambda: gen.sleep(0.05)) try: buf = bytearray(10) fut = rs.read_into(buf) ws.write(b"hello") yield gen.sleep(0.05) self.assertTrue(rs.reading()) ws.write(b"world!!") data = yield fut self.assertFalse(rs.reading()) self.assertEqual(data, 10) self.assertEqual(bytes(buf), b"helloworld") # Existing buffer is fed into user buffer fut = rs.read_into(buf) yield gen.sleep(0.05) self.assertTrue(rs.reading()) ws.write(b"1234567890") data = yield fut self.assertFalse(rs.reading()) self.assertEqual(data, 10) self.assertEqual(bytes(buf), b"!!12345678") # Existing buffer can satisfy read immediately buf = bytearray(4) ws.write(b"abcdefghi") data = yield rs.read_into(buf) self.assertEqual(data, 4) self.assertEqual(bytes(buf), b"90ab") data = yield rs.read_bytes(7) self.assertEqual(data, b"cdefghi") finally: ws.close() rs.close()
Example #25
Source File: queues_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_get_clears_timed_out_getters(self): q = queues.Queue() # type: queues.Queue[int] getters = [ asyncio.ensure_future(q.get(timedelta(seconds=0.01))) for _ in range(10) ] get = asyncio.ensure_future(q.get()) self.assertEqual(11, len(q._getters)) yield gen.sleep(0.02) self.assertEqual(11, len(q._getters)) self.assertFalse(get.done()) # Final waiter is still active. q.get() # get() clears the waiters. self.assertEqual(2, len(q._getters)) for getter in getters: self.assertRaises(TimeoutError, getter.result)
Example #26
Source File: queues_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_put_timeout_preempted(self): q = queues.Queue(1) # type: queues.Queue[int] q.put_nowait(0) put = q.put(1, timeout=timedelta(seconds=0.01)) q.get() yield gen.sleep(0.02) yield put # No TimeoutError.
Example #27
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_timeout_concurrent_future(self): # A concurrent future that does not resolve before the timeout. with futures.ThreadPoolExecutor(1) as executor: with self.assertRaises(gen.TimeoutError): yield gen.with_timeout( self.io_loop.time(), executor.submit(time.sleep, 0.1) )
Example #28
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_normal_concurrent_future(self): # A conccurrent future that resolves while waiting for the timeout. with futures.ThreadPoolExecutor(1) as executor: yield gen.with_timeout( datetime.timedelta(seconds=3600), executor.submit(lambda: time.sleep(0.01)), )
Example #29
Source File: gen_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_no_ref(self): # In this usage, there is no direct hard reference to the # WaitIterator itself, only the Future it returns. Since # WaitIterator uses weak references internally to improve GC # performance, this used to cause problems. yield gen.with_timeout( datetime.timedelta(seconds=0.1), gen.WaitIterator(gen.sleep(0)).next() )
Example #30
Source File: testing_test.py From opendevops with GNU General Public License v3.0 | 5 votes |
def test_no_timeout(self): # A test that does not exceed its timeout should succeed. @gen_test(timeout=1) def test(self): yield gen.sleep(0.1) test(self) self.finished = True