def wait(future: Future): """Wait future object finished. :param future: Future object generated by run_in_executor*() """ sem = BoundedSemaphore() sem.acquire() future.add_done_callback( partial(lambda future, sem: sem.release(), sem=sem)) with sem: return
class _GreenAbstractTransport(_AbstractTransport): """ Transport class that assures that sending or receiving a single frame is an atomic greenlet operation. """ def __init__(self, *args, **kw): self._green_read_lock = BoundedSemaphore(1, 1) self._green_write_lock = BoundedSemaphore(1, 1) _AbstractTransport.__init__(self, *args, **kw) def read_frame(self): self._green_read_lock.acquire() try: return _AbstractTransport.read_frame(self) finally: self._green_read_lock.release() def write_frame(self, frame_type, channel, payload): self._green_write_lock.acquire() try: return _AbstractTransport.write_frame(self, frame_type, channel, payload) finally: self._green_write_lock.release()
def __init__(self, *args, **kwargs): super(ContextStackManagerEventletMixin, self).__init__(*args, **kwargs) try: from eventlet.corolocal import local from eventlet.semaphore import BoundedSemaphore except ImportError: raise RuntimeError('the eventlet library is required for %s' % self.__class__.__name__) self._coroutine_context = local() self._contexts.append(self._coroutine_context) self._coroutine_lock = BoundedSemaphore()
def wait_all(futures: List[Future]): """Wait all futures done :param futures: Futures list """ sem = BoundedSemaphore(len(futures)) for future in futures: sem.acquire() future.add_done_callback( partial(lambda future, sem: sem.release(), sem=sem)) while True: try: sem.release(blocking=False) sem.acquire() eventlet.sleep(0.05) except ValueError: break
def __init__(self, *args, **kw): self._green_read_lock = BoundedSemaphore(1, 1) self._green_write_lock = BoundedSemaphore(1, 1) _AbstractTransport.__init__(self, *args, **kw)