本文整理匯總了Python中tornado.locks.Semaphore方法的典型用法代碼示例。如果您正苦於以下問題:Python locks.Semaphore方法的具體用法?Python locks.Semaphore怎麽用?Python locks.Semaphore使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tornado.locks
的用法示例。
在下文中一共展示了locks.Semaphore方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_acquire
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [as 別名]
def test_acquire(self):
sem = locks.Semaphore()
f0 = sem.acquire()
self.assertTrue(f0.done())
# Wait for release().
f1 = sem.acquire()
self.assertFalse(f1.done())
f2 = sem.acquire()
sem.release()
self.assertTrue(f1.done())
self.assertFalse(f2.done())
sem.release()
self.assertTrue(f2.done())
sem.release()
# Now acquire() is instant.
self.assertTrue(sem.acquire().done())
self.assertEqual(0, len(sem._waiters))
示例2: test_garbage_collection
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [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)
示例3: test_context_manager_contended
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [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)
示例4: test_acquire
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [as 別名]
def test_acquire(self):
sem = locks.Semaphore()
f0 = asyncio.ensure_future(sem.acquire())
self.assertTrue(f0.done())
# Wait for release().
f1 = asyncio.ensure_future(sem.acquire())
self.assertFalse(f1.done())
f2 = asyncio.ensure_future(sem.acquire())
sem.release()
self.assertTrue(f1.done())
self.assertFalse(f2.done())
sem.release()
self.assertTrue(f2.done())
sem.release()
# Now acquire() is instant.
self.assertTrue(asyncio.ensure_future(sem.acquire()).done())
self.assertEqual(0, len(sem._waiters))
示例5: test_garbage_collection
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [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)
示例6: test_context_manager_contended
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [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)
示例7: __init__
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [as 別名]
def __init__(self, value=1):
super(Semaphore, self).__init__()
if value < 0:
raise ValueError('semaphore initial value must be >= 0')
self._value = value
示例8: __repr__
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [as 別名]
def __repr__(self):
res = super(Semaphore, self).__repr__()
extra = 'locked' if self._value == 0 else 'unlocked,value:{0}'.format(
self._value)
if self._waiters:
extra = '{0},waiters:{1}'.format(extra, len(self._waiters))
return '<{0} [{1}]>'.format(res[1:-1], extra)
示例9: __enter__
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [as 別名]
def __enter__(self):
raise RuntimeError(
"Use Semaphore like 'with (yield semaphore.acquire())', not like"
" 'with semaphore'")
示例10: release
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [as 別名]
def release(self):
"""增加counter 並且喚醒一個waiter."""
if self._value >= self._initial_value:
raise ValueError("Semaphore released too many times")
super(BoundedSemaphore, self).release()
示例11: test_negative_value
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [as 別名]
def test_negative_value(self):
self.assertRaises(ValueError, locks.Semaphore, value=-1)
示例12: test_acquire_timeout
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [as 別名]
def test_acquire_timeout(self):
sem = locks.Semaphore(2)
yield sem.acquire()
yield sem.acquire()
acquire = sem.acquire(timedelta(seconds=0.01))
self.io_loop.call_later(0.02, sem.release) # Too late.
yield gen.sleep(0.3)
with self.assertRaises(gen.TimeoutError):
yield acquire
sem.acquire()
f = sem.acquire()
self.assertFalse(f.done())
sem.release()
self.assertTrue(f.done())
示例13: test_acquire_timeout_preempted
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [as 別名]
def test_acquire_timeout_preempted(self):
sem = locks.Semaphore(1)
yield sem.acquire()
# This fires before the wait times out.
self.io_loop.call_later(0.01, sem.release)
acquire = sem.acquire(timedelta(seconds=0.02))
yield gen.sleep(0.03)
yield acquire # No TimeoutError.
示例14: test_release_unacquired
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [as 別名]
def test_release_unacquired(self):
# Unbounded releases are allowed, and increment the semaphore's value.
sem = locks.Semaphore()
sem.release()
sem.release()
# Now the counter is 3. We can acquire three times before blocking.
self.assertTrue(sem.acquire().done())
self.assertTrue(sem.acquire().done())
self.assertTrue(sem.acquire().done())
self.assertFalse(sem.acquire().done())
示例15: test_context_manager_async_await
# 需要導入模塊: from tornado import locks [as 別名]
# 或者: from tornado.locks import Semaphore [as 別名]
def test_context_manager_async_await(self):
# Repeat the above test using 'async with'.
sem = locks.Semaphore()
namespace = exec_test(globals(), locals(), """
async def f():
async with sem as yielded:
self.assertTrue(yielded is None)
""")
yield namespace['f']()
# Semaphore was released and can be acquired again.
self.assertTrue(sem.acquire().done())