Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiprocessing Queue subclass issue

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:

like image 387
SkyLeach Avatar asked Dec 15 '15 14:12

SkyLeach


People also ask

Is multiprocessing queue process safe?

Python provides a number of process-safe queues, such as the multiprocessing. Queue class.

Is multiprocessing queue slow?

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.

What purpose does the multiprocessing queue class serve?

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.

Is multiprocessing value thread safe?

Yes, it is. From https://docs.python.org/3/library/multiprocessing.html#exchanging-objects-between-processes: Queues are thread and process safe.


1 Answers

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()

like image 99
eugecm Avatar answered Oct 04 '22 01:10

eugecm