def dequeue(self): ''' Remove and return the value stored at the front of the queue ''' if self._size == 0: raise Empty('Queue is empty') self._data[self._front] = None self._front = (self._front + 1) % len(self._data) self._size -= 1
def get_next_task(self): """ Implementing classes or mix ins should override this method. By default, this method will cause an Actor to idle by raising an <code>Empty</code> exception when invoked. :raises Empty: if no next task is available. """ raise Empty()
def top(self): """Return (but do not remove) the element at the top of the stack. Raise Empty exception if the stack is empty. """ if self.is_empty(): raise Empty('Stack is empty') return self._data[-1] # the last item in the list
def pop(self): """Remove and return the element from the top of the stack (i.e., LIFO). Raise Empty exception if the stack is empty. """ if self.is_empty(): raise Empty('Stack is empty') return self._data.pop()
def test_eventual_deadline_iopub(self, preprocessor, cell_mock, message_mock): # Process a few messages before raising a timeout from iopub message_mock.side_effect = list( message_mock.side_effect)[:-1] + [Empty()] preprocessor.kc.shell_channel.get_msg = MagicMock( return_value={'parent_header': { 'msg_id': preprocessor.parent_id }}) preprocessor.raise_on_iopub_timeout = True with pytest.raises(TimeoutError): preprocessor.run_cell(cell_mock) assert message_mock.call_count == 3 # Ensure the output was captured self.assertListEqual(cell_mock.outputs, [{ 'output_type': 'stream', 'name': 'stdout', 'text': 'foo' }, { 'output_type': 'stream', 'name': 'stderr', 'text': 'bar' }])
def _zrem_read(self, **options): try: try: # The last response contains the response of ZRANGE. output = None for i in range(0, 4): output = self.client.parse_response( self.client.connection, '_', **options) item = output[0] if len(item) == 0: item = None else: item = self._remove_time_prefix(item[0]) except self.connection_errors: # if there's a ConnectionError, disconnect so the next # iteration will reconnect automatically. self.client.connection.disconnect() raise # Rotate the queue dest = self.queue all_queues_empty = self._queue_scheduler.rotate( dest, not bool(item)) if item: self.connection._deliver(loads(bytes_to_str(item)), dest) return True elif all_queues_empty: sleep(1) raise Empty() finally: self._in_poll = None
def enqueue(self, e): ''' Insert e at the end of the queue ''' if self._size == self._data: raise Empty('Queue is full') avail = (self._front + self._size) % len(self._data) self._data[avail] = e self._size += 1
def pop(self): if self.is_empty(): raise Empty('Stack is empty') answer = self._head._element self._head = self._head._next self._size -= 1 return answer
def _get( self, queue: str, timeout: Optional[Union[float, int]] = None ) -> Dict[str, Any]: """Try to retrieve a single message off ``queue``.""" # If we're not ack'ing for this queue, just change receive_mode recv_mode = ServiceBusReceiveMode.RECEIVE_AND_DELETE \ if queue in self._noack_queues else ServiceBusReceiveMode.PEEK_LOCK queue = self.entity_name(self.queue_name_prefix + queue) queue_obj = self._get_asb_receiver(queue, recv_mode) messages = queue_obj.receiver.receive_messages( max_message_count=1, max_wait_time=timeout or self.wait_time_seconds) if not messages: raise Empty() # message.body is either byte or generator[bytes] message = messages[0] if not isinstance(message.body, bytes): body = b''.join(message.body) else: body = message.body msg = loads(bytes_to_str(body)) msg['properties']['delivery_info']['azure_message'] = message msg['properties']['delivery_info']['azure_queue_name'] = queue return msg
def get(self, callback, timeout=None): self._in_protected_read = True try: for channel in self._channels: if channel.active_queues: # BRPOP mode? if channel.qos.can_consume(): self._register_BRPOP(channel) if channel.active_fanout_queues: # LISTEN mode? self._register_LISTEN(channel) events = self.poller.poll(timeout) if events: for fileno, event in events: ret = self.handle_event(fileno, event) if ret: return # - no new data, so try to restore messages. # - reset active redis commands. self.maybe_restore_messages() raise Empty() finally: self._in_protected_read = False while self.after_read: try: fun = self.after_read.pop() except KeyError: break else: fun()
def _get(self, queue): with self.conn_or_acquire() as client: for pri in self.priority_steps: item = client.rpop(self._q_for_pri(queue, pri)) if item: return loads(bytes_to_str(item)) raise Empty()
def get(self, block=True, timeout=0): if block: timeout = None sema, fd = self.get_input() #if self._pid == self._initpid: # print "< GET ", getpid(), self.serial, self.getcount, '-' #else: # print " GET >", getpid(), self.serial, self.getcount, '-' if not self.down(sema, timeout=timeout): raise Empty() flock(fd, LOCK_EX) try: header = read_all(fd, 48) chk = header[16:] header = header[:16] size = int(header, 16) data = read_all(fd, size) pos = lseek(fd, 0, SEEK_CUR) if pos > 1048576: st = fstat(fd) if pos >= st.st_size: ftruncate(fd, 0) lseek(fd, 0, SEEK_SET) finally: flock(fd, LOCK_UN) _chk = sha256(data).digest() if chk != _chk: raise AssertionError("Corrupt Data!") obj = marshal_loads(data) self.getcount += 1 return obj
def solve(N, sides): def calc(state, side): s = 0 for i in range(N): s += sides[i] return s - side * sideL def can(side, state): left = calc(state, side) if left == sideL: return can(side + 1, state) elif side == 4: return 1 elif dp[side, state] != -1: return dp[side, state] else: result = 0 for i in range(N): if not (state & (1 << i)) and left + sides[i] <= sideL: result |= can(side, state | (1 << i)) dp[side, state] = result return result #N,sides = par s = sum(sides) if s % 4 != 0: return 'no' sideL = s // 4 dp = Empty((4, 1 << 21), dtype=Bytes) dp.fill(-1) if can(0, 0): return 'yes' else: return 'no'
def test_deadline_iopub(self, preprocessor, cell_mock, message_mock): # The shell_channel will complete, so we expect only to hit the iopub timeout. message_mock.side_effect = Empty() preprocessor.raise_on_iopub_timeout = True with pytest.raises(TimeoutError): preprocessor.run_cell(cell_mock)
def _receive_one(self, c): response = None try: response = c.parse_response() except self.connection_errors: self._in_listen = None raise if isinstance(response, (list, tuple)): payload = self._handle_message(c, response) if bytes_to_str(payload['type']).endswith('message'): channel = bytes_to_str(payload['channel']) if payload['data']: if channel[0] == '/': _, _, channel = channel.partition('.') try: message = loads(bytes_to_str(payload['data'])) except (TypeError, ValueError): warn('Cannot process event on channel %r: %s', channel, repr(payload)[:4096], exc_info=1) raise Empty() exchange = channel.split('/', 1)[0] self.connection._deliver(message, self._fanout_to_queue[exchange]) return True
def pop_nowait(self) -> Any: """ Pops the latest value for the stored variable without waiting. If no value has been set yet, raises an Empty exception. Returns ------- Any Latest value for the stored variable. Raises ------ Empty If no value for the stored variable exists. """ with self._cond: try: if not self._has_value: raise Empty() else: return self._value finally: self._value = None self._has_value = False
def pop(self, timeout: Optional[float] = None) -> Any: """ Pop the latest value for the stored variable. If timeout is None (the default), block until a value is available. Otherwise, block for up to timeout seconds, after which an Empty exception is raised if no value was made available within that time. Parameters ---------- timeout Number of seconds to block for. If None, blocks indefinitely. Returns ------- Any The stored value. Raises ------ Empty If timeout is not None and no value is available when it runs out. """ with self._cond: try: while not self._has_value: if not self._cond.wait(timeout=timeout): raise Empty() return self._value finally: self._value = None self._has_value = False
def drain_events(self, timeout=None, callback=None): callback = callback or self.connection._deliver if self._consumers and self.qos.can_consume(): if hasattr(self, '_get_many'): return self._get_many(self._active_queues, timeout=timeout) return self._poll(self.cycle, callback, timeout=timeout) raise Empty()
def last(self): ''' Return the value stored at the last of the Deque ''' if self.is_empty(): raise Empty("Queue is Empty") loc = (self._front + self._size - 1) % len(self._data) ans = self._data[loc] return ans
def first(self): """Return (but do not remove) the element at the front of the queue. Raise Empty exception if the queue is empty. """ if self.is_empty(): raise Empty('Queue is empty') return self._data[self._front]
def pop(self): # O(1) if self.is_empty(): raise Empty() self._top = (self._top - 1) % len(self._data) out = self._data[self._top] self._data[self._top] = None self._size -= 1 return out
def delete_last(self): if self.is_empty(): raise Empty("Deque is Empty") loc = (self._front + self._size - 1) % len(self._data) ans = self._data[loc] self._data[loc] = None self._size -= 1 return ans
def dequeue(self): if self.is_empty(): raise Empty() to_return = self._data[self._front] self._data[self._front] = None self._front = (self._front + 1) % len(self._data) self._size -= 1 return to_return
def first(self): """Return (but do not remove) the element at the front of the queue. Raise Empty exception if the queue is empty. """ if self.is_empty(): raise Empty('Queue is empty') return self._head._element # front aligned with head of list
def _get(self, queue): queue = self._get_queue(queue) msg = queue.get() if msg is None: raise Empty() return loads(bytes_to_str(msg))
def delete_first(self): if self.is_empty(): raise Empty("Deque is Empty") ans = self._data[self._front] self._data[self._front] = None self._front = (self._front + 1) % len(self._data) self._size -= 1 return ans
def get(self, block=True, timeout=None): r = Queue.get(self, block, timeout) self.task_done() if isinstance(r, _EndOfQueue): self.put(r) raise Empty('future has been closed') else: return r
def get_nowait(self): # Need multiprocessing queues here for q in self.queues: try: return q.get_nowait() except Empty: pass raise Empty()
def create_jobs(ITEMS, queue): if not ITEMS: from queue import Empty raise Empty() for item in ITEMS: queue.put(item) queue.join() return
def _get_queue_event(cls, queue: Queue, timeout: float) -> Event: """Internal. Get next queued event, with timeout.""" try: event = queue.get(block=True, timeout=timeout) except Empty: raise Empty("Timed out while waiting for stream") return event