예제 #1
0
파일: transport.py 프로젝트: jab/greenamqp
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 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
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