This repository has been archived on 2024-11-28. You can view files and clone it, but cannot push or open issues or pull requests.
PyBitmessage-2024-11-28/src/multiqueue.py

55 lines
1.4 KiB
Python
Raw Normal View History

"""
2019-12-23 10:49:03 +01:00
A queue with multiple internal subqueues.
Elements are added into a random subqueue, and retrieval rotates
"""
from collections import deque
from six.moves import queue
try:
2021-03-04 15:15:41 +01:00
import helper_random
except ImportError:
from . import helper_random
class MultiQueue(queue.Queue):
"""A base queue class"""
# pylint: disable=redefined-builtin,attribute-defined-outside-init
defaultQueueCount = 10
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 = []
for _ in range(self.queueCount):
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):
# self.queue.append(item)
self.queues[helper_random.randomrandrange(self.queueCount)].append(
(item))
# Get an item from the queue
def _get(self):
return self.queues[self.iter].popleft()
def iterate(self):
"""Increment the iteration counter"""
self.iter = (self.iter + 1) % self.queueCount
def totalSize(self):
"""Return the total number of items in all the queues"""
return sum(len(x) for x in self.queues)