def __init__(self, maxsize=0): # Cannot use super() here because Queue in Python2 is old style class Queue.__init__(self, maxsize) # Number of item dropped (queue full) self.dropped = 0 # Number of items accepted self.accepted = 0 # Cumulative length of accepted items self.accepted_lengths = 0
def put(self, item): try: # Cannot use super() here because Queue in Python2 is old style class Queue.put(self, item, block=False) except Full: # If the queue is full, replace a random item. We need to make sure # the queue is not emptied was emptied in the meantime, so we lock # check qsize value. with self.mutex: qsize = self._qsize() if qsize != 0: idx = random.randrange(0, qsize) self.queue[idx] = item log.warning("Writer queue is full has more than %d traces, some traces will be lost", self.maxsize) self.dropped += 1 self._update_stats(item) return # The queue has been emptied, simply retry putting item return self.put(item) else: with self.mutex: self._update_stats(item)
def put(self, item): try: # Cannot use super() here because Queue in Python2 is old style class return Queue.put(self, item, block=False) except Full: # If the queue is full, replace a random item. We need to make sure # the queue is not emptied was emptied in the meantime, so we lock # check qsize value. with self.mutex: qsize = self._qsize() if qsize != 0: idx = random.randrange(0, qsize) self.queue[idx] = item return # The queue has been emptied, simply retry putting item return self.put(item)