本文整理匯總了Python中tornado.gen.TimeoutError方法的典型用法代碼示例。如果您正苦於以下問題:Python gen.TimeoutError方法的具體用法?Python gen.TimeoutError怎麽用?Python gen.TimeoutError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tornado.gen
的用法示例。
在下文中一共展示了gen.TimeoutError方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_garbage_collection
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
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)
示例2: put
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def put(
self, item: _T, timeout: Union[float, datetime.timedelta] = None
) -> "Future[None]":
"""Put an item into the queue, perhaps waiting until there is room.
Returns a Future, which raises `tornado.util.TimeoutError` after a
timeout.
``timeout`` may be a number denoting a time (on the same
scale as `tornado.ioloop.IOLoop.time`, normally `time.time`), or a
`datetime.timedelta` object for a deadline relative to the
current time.
"""
future = Future() # type: Future[None]
try:
self.put_nowait(item)
except QueueFull:
self._putters.append((item, future))
_set_timeout(future, timeout)
else:
future.set_result(None)
return future
示例3: test_garbage_collection
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def test_garbage_collection(self):
# Test that timed-out waiters are occasionally cleaned from the queue.
sem = locks.Semaphore(value=0)
futures = [
asyncio.ensure_future(sem.acquire(timedelta(seconds=0.01)))
for _ in range(101)
]
future = asyncio.ensure_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)
示例4: test_put_clears_timed_out_getters
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
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)
示例5: put
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def put(self, item, timeout=None):
"""Put an item into the queue, perhaps waiting until there is room.
Returns a Future, which raises `tornado.util.TimeoutError` after a
timeout.
``timeout`` may be a number denoting a time (on the same
scale as `tornado.ioloop.IOLoop.time`, normally `time.time`), or a
`datetime.timedelta` object for a deadline relative to the
current time.
"""
future = Future()
try:
self.put_nowait(item)
except QueueFull:
self._putters.append((item, future))
_set_timeout(future, timeout)
else:
future.set_result(None)
return future
示例6: get
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def get(self, timeout=None):
"""Remove and return an item from the queue.
Returns a Future which resolves once an item is available, or raises
`tornado.util.TimeoutError` after a timeout.
``timeout`` may be a number denoting a time (on the same
scale as `tornado.ioloop.IOLoop.time`, normally `time.time`), or a
`datetime.timedelta` object for a deadline relative to the
current time.
"""
future = Future()
try:
future.set_result(self.get_nowait())
except QueueEmpty:
self._getters.append(future)
_set_timeout(future, timeout)
return future
示例7: wait
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def wait(self, timeout=None):
"""Block until the internal flag is true.
Returns a Future, which raises `tornado.util.TimeoutError` after a
timeout.
"""
fut = Future()
if self._value:
fut.set_result(None)
return fut
self._waiters.add(fut)
fut.add_done_callback(lambda fut: self._waiters.remove(fut))
if timeout is None:
return fut
else:
timeout_fut = gen.with_timeout(timeout, fut, quiet_exceptions=(CancelledError,))
# This is a slightly clumsy workaround for the fact that
# gen.with_timeout doesn't cancel its futures. Cancelling
# fut will remove it from the waiters list.
timeout_fut.add_done_callback(lambda tf: fut.cancel() if not fut.done() else None)
return timeout_fut
示例8: acquire
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def acquire(self, timeout=None):
"""Decrement the counter. Returns a Future.
Block if the counter is zero and wait for a `.release`. The Future
raises `.TimeoutError` after the deadline.
"""
waiter = Future()
if self._value > 0:
self._value -= 1
waiter.set_result(_ReleasingContextManager(self))
else:
self._waiters.append(waiter)
if timeout:
def on_timeout():
if not waiter.done():
waiter.set_exception(gen.TimeoutError())
self._garbage_collect()
io_loop = ioloop.IOLoop.current()
timeout_handle = io_loop.add_timeout(timeout, on_timeout)
waiter.add_done_callback(
lambda _: io_loop.remove_timeout(timeout_handle))
return waiter
示例9: _set_timeout
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def _set_timeout(future, timeout):
if timeout:
def on_timeout():
future.set_exception(gen.TimeoutError())
io_loop = ioloop.IOLoop.current()
timeout_handle = io_loop.add_timeout(timeout, on_timeout)
future.add_done_callback(
lambda _: io_loop.remove_timeout(timeout_handle))
示例10: put
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def put(self, item, timeout=None):
"""將一個項目放入隊列中, 可能需要等待直到隊列中有空間.
返回一個Future對象, 如果超時會拋出 `tornado.gen.TimeoutError` .
"""
try:
self.put_nowait(item)
except QueueFull:
future = Future()
self._putters.append((item, future))
_set_timeout(future, timeout)
return future
else:
return gen._null_future
示例11: get
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def get(self, timeout=None):
"""從隊列中刪除並返回一個項目.
返回一個Future對象, 當項目可用時resolve, 或者在超時後拋出
`tornado.gen.TimeoutError` .
"""
future = Future()
try:
future.set_result(self.get_nowait())
except QueueEmpty:
self._getters.append(future)
_set_timeout(future, timeout)
return future
示例12: join
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def join(self, timeout=None):
"""阻塞(block)直到隊列中的所有項目都處理完.
返回一個Future對象, 超時後會拋出 `tornado.gen.TimeoutError` 異常.
"""
return self._finished.wait(timeout)
示例13: wait
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def wait(self, timeout=None):
"""阻塞直到內部標識為true.
返回一個Future對象, 在超時之後會拋出一個 `tornado.gen.TimeoutError`
異常.
"""
if timeout is None:
return self._future
else:
return gen.with_timeout(timeout, self._future)
示例14: acquire
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def acquire(self, timeout=None):
"""嘗試鎖. 返回一個Future 對象.
返回一個Future 對象, 在超時之後將拋出
`tornado.gen.TimeoutError` .
"""
return self._block.acquire(timeout)
示例15: test_timeout
# 需要導入模塊: from tornado import gen [as 別名]
# 或者: from tornado.gen import TimeoutError [as 別名]
def test_timeout(self):
with self.assertRaises(gen.TimeoutError):
yield gen.with_timeout(datetime.timedelta(seconds=0.1),
Future())