Пример #1
0
    def wait(self, timeout=None):
        """pause the current coroutine until this event is set

        if :meth:`set` method has been called, this method will not block at
        all. otherwise it will block until :meth:`set` method is called

        :param timeout:
            the maximum amount of time to block in seconds. the default of
            ``None`` allows indefinite blocking.
        :type timeout: number or None

        :returns:
            ``True`` if a timeout was provided and was hit, otherwise ``False``
        """
        if self._is_set:
            return False

        current = compat.getcurrent()  # the waiting greenlet

        waketime = None if timeout is None else time.time() + timeout
        if timeout is not None:
            scheduler.schedule_at(waketime, current)

        self._waiters.append(current)
        scheduler.state.mainloop.switch()

        if timeout is not None:
            timedout = not scheduler._remove_from_timedout(waketime, current)
            if timedout:
                self._waiters.remove(current)
            return timedout

        return False
Пример #2
0
    def wait(self, timeout=None):
        """wait to be woken up by the condition

        :raises:
            `RuntimeError` if the underlying lock hasn't been
            :meth:`acquired <Lock.acquire>`
        """
        if not self._is_owned():
            raise RuntimeError("cannot wait on un-acquired lock")

        current = compat.getcurrent()

        waketime = None if timeout is None else time.time() + timeout
        if timeout is not None:
            scheduler.schedule_at(waketime, current)
        self._waiters.append((current, waketime))

        self._lock.release()
        scheduler.state.mainloop.switch()
        self._lock.acquire()

        if timeout is not None:
            timedout = not scheduler._remove_from_timedout(waketime, current)
            if timedout:
                self._waiters.remove((current, waketime))
            return timedout

        return False
Пример #3
0
    def put(self, item, blocking=True, timeout=None):
        """put an item into the queue

        if the queue was instantiated with a nonzero `maxsize` and that size
        has already been reached, this method will block until another greenlet
        :meth:`get`\ s an item out

        :param item: the object to put into the queue, can be any type
        :param blocking:
            whether to block if the queue is already :meth:`full` (default
            ``True``)
        :type blocking: bool
        :param timeout:
            the maximum time in seconds to block waiting. with the default of
            ``None``, it can wait indefinitely. this is unused if `blocking` is
            ``False``.
        :type timeout: int, float or None

        :raises:
            :class:`Full` if the queue is :meth:`full` and `blocking` is
            ``False``, or if `timeout` expires.
        """
        if self.full():
            if not blocking:
                raise Full()

            current = compat.getcurrent()

            waketime = None if timeout is None else time.time() + timeout
            if timeout is not None:
                scheduler.schedule_at(waketime, current)
            self._waiters.append((current, waketime))

            scheduler.state.mainloop.switch()

            if timeout is not None:
                if not scheduler._remove_from_timedout(waketime, current):
                    self._waiters.remove((current, waketime))
                    raise Full()

        if self._waiters and not self.full():
            scheduler.schedule(self._waiters.popleft()[0])

        if not self._open_tasks:
            self._jobs_done.clear()
        self._open_tasks += 1

        self._put(item)
Пример #4
0
    def get(self, blocking=True, timeout=None):
        """get an item out of the queue

        this method will block if `blocking` is ``True`` (default) and the
        queue is :meth:`empty`

        :param blocking:
            whether to block if there is no data yet available (default
            ``True``)
        :type blocking: bool
        :param timeout:
            the maximum time in seconds to block waiting for data. with the
            default of ``None``, can wait indefinitely. this is unused if
            `blocking` is ``False``.
        :type timeout: int, float or None

        :raises:
            :class:`Empty` if there is no data in the queue and blocking is
            ``False``, or `timeout` expires

        :returns: something that was previously :meth:`put` in the queue
        """
        if not self._data:
            if not blocking:
                raise Empty()

            current = compat.getcurrent()

            waketime = None if timeout is None else time.time() + timeout
            if timeout is not None:
                scheduler.schedule_at(waketime, current)
            self._waiters.append((current, waketime))

            scheduler.state.mainloop.switch()

            if timeout is not None:
                if not scheduler._remove_from_timedout(waketime, current):
                    self._waiters.remove((current, waketime))
                    raise Empty()

        if self.full() and self._waiters:
            scheduler.schedule(self._waiters.popleft()[0])

        return self._get()