2018-10-21 18:14:20 +02:00
|
|
|
"""
|
|
|
|
src/multiqueue.py
|
|
|
|
=================
|
|
|
|
"""
|
|
|
|
|
2017-06-27 13:25:12 +02:00
|
|
|
import Queue
|
2018-10-21 18:14:20 +02:00
|
|
|
from collections import deque
|
|
|
|
|
2018-03-21 13:49:08 +01:00
|
|
|
import helper_random
|
2017-06-27 13:25:12 +02:00
|
|
|
|
2018-10-21 18:14:20 +02:00
|
|
|
|
2017-06-27 13:25:12 +02:00
|
|
|
class MultiQueue(Queue.Queue):
|
2018-10-21 18:14:20 +02:00
|
|
|
"""A base queue class"""
|
|
|
|
# pylint: disable=redefined-builtin,attribute-defined-outside-init
|
2017-06-27 13:25:12 +02:00
|
|
|
defaultQueueCount = 10
|
2018-10-21 18:14:20 +02:00
|
|
|
|
2017-06-27 13:25:12 +02:00
|
|
|
def __init__(self, maxsize=0, count=0):
|
|
|
|
if not count:
|
|
|
|
self.queueCount = MultiQueue.defaultQueueCount
|
|
|
|
else:
|
|
|
|
self.queueCount = count
|
|
|
|
Queue.Queue.__init__(self, maxsize)
|
|
|
|
|
|
|
|
# Initialize the queue representation
|
|
|
|
def _init(self, maxsize):
|
|
|
|
self.iter = 0
|
|
|
|
self.queues = []
|
2018-10-21 18:14:20 +02:00
|
|
|
for _ in range(self.queueCount):
|
2017-06-27 13:25:12 +02:00
|
|
|
self.queues.append(deque())
|
|
|
|
|
|
|
|
def _qsize(self, len=len):
|
|
|
|
return len(self.queues[self.iter])
|
|
|
|
|
|
|
|
# Put a new item in the queue
|
|
|
|
def _put(self, item):
|
2018-10-21 18:14:20 +02:00
|
|
|
# self.queue.append(item)
|
2018-03-21 13:49:08 +01:00
|
|
|
self.queues[helper_random.randomrandrange(self.queueCount)].append((item))
|
2017-06-27 13:25:12 +02:00
|
|
|
|
|
|
|
# Get an item from the queue
|
|
|
|
def _get(self):
|
|
|
|
return self.queues[self.iter].popleft()
|
|
|
|
|
|
|
|
def iterate(self):
|
2018-10-21 18:14:20 +02:00
|
|
|
"""Increment the iteration counter"""
|
2017-06-27 13:25:12 +02:00
|
|
|
self.iter = (self.iter + 1) % self.queueCount
|
2017-10-19 08:56:48 +02:00
|
|
|
|
|
|
|
def totalSize(self):
|
2018-10-21 18:14:20 +02:00
|
|
|
"""Return the total number of items in all the queues"""
|
2017-10-19 08:56:48 +02:00
|
|
|
return sum(len(x) for x in self.queues)
|