def get(self, block=True, timeout=None): """Retrieve the task from the first available bucket. Available as in, there is an item in the queue and you can consume tokens from it. """ tstart = time() get = self._get not_empty = self.not_empty with not_empty: while 1: try: remaining_time, item = get() except Empty: if not block or (timeout and time() - tstart > timeout): raise not_empty.wait(timeout) continue if remaining_time: if not block or (timeout and time() - tstart > timeout): raise Empty() sleep(min(remaining_time, timeout or 1)) else: return item
def test_poll_readable_raises_Empty(self): x = X(self.app) x.hub.readers = {6: Mock()} x.close_then_error(x.connection.drain_nowait) x.hub.poller.poll.return_value = [(6, READ)] x.hub.readers[6].side_effect = Empty() with self.assertRaises(socket.error): asynloop(*x.args) x.hub.readers[6].assert_called_with(6, READ) self.assertTrue(x.hub.poller.poll.called)
def test_poll_readable_raises_Empty(self): x = X(self.app) reader = Mock(name='reader') x.hub.add_reader(6, reader, 6) x.hub.on_tick.add(x.close_then_error(Mock(name='tick'), 2)) x.hub.poller.poll.return_value = [(6, READ)] reader.side_effect = Empty() with self.assertRaises(socket.error): asynloop(*x.args) reader.assert_called_with(6) self.assertTrue(x.hub.poller.poll.called)
def get(self, block=True): """Remove and return an item from the queue. :raises RateLimitExceeded: If a token could not be consumed from the token bucket (consuming from the queue too fast). :raises Queue.Empty: If an item is not immediately available. """ get = block and self.queue.get or self.queue.get_nowait if not block and not self.items: raise Empty() if not self._bucket.can_consume(1): raise RateLimitExceeded() return get()
def _get_immediate(self): try: return self.immediate.popleft() except IndexError: raise Empty()
def effect(): if get.call_count == 1: raise Empty() rem = remaining[0] remaining[0] = 0 return rem, Mock()