I want to subclass multiprocessing.Queue for implementing processes to grab chunks of the queue. The only problem is, I'm getting a weird TypeError?
#!/usr/bin/env python
#whaaaaa!?
from multiprocessing import Queue
class BufferQueue(Queue):
'''A thread/process safe queue for append/popleft operations with the import
buffer.'''
def __init__(self, **kwargs):
super(BufferQueue,self).__init__(**kwargs)
def consume(self, lim):
'''Consume up to but no more than lim elements and return them in a new
list, cleaning up the buffer.
@params
lim -- the maximum (limit) to consume from the list. If less items
exist in the list then that's fine too.
'''
lim = len(queue) if len(queue) < lim else lim
return [self.popleft() for i in range(lim)]
testing this (I split this out so that I wasn't pulling in anything else)
| => ./tests/wtf_queue.py
Traceback (most recent call last):
File "./tests/wtf_queue.py", line 10, in <module>
class BufferQueue(Queue):
TypeError: method expected 2 arguments, got 3
Edit/Update:
Python provides a number of process-safe queues, such as the multiprocessing. Queue class.
In other words, Multiprocess queue is pretty slow putting and getting individual data, then QuickQueue wrap several data in one list, this list is one single data that is enqueue in the queue than is more quickly than put one individual data.
Overview: The Queue class in Multiprocessing module of Python Standard Library provides a mechanism to pass data between a parent process and the descendent processes of it.
Yes, it is. From https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes: Queues are thread and process safe.
multiprocessing.Queue
is a method that creates queues, so you're supposed to use it as a function my_queue = Queue()
.
>>> from multiprocessing import Queue
>>> type(Queue)
<class 'method'>
As you can see is not a 'type', which you would use to subclass.
If you want to implement your own queue, you could take a look at queue.Queue
EDIT:
If you want to subclass the queue from multiprocessing, use multiprocessing.queues.Queue
instead, which is the type of the object returned by multiprocessing.Queue()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With