Queue subclass multiprocessing

I want a subclass of multiprocessing .Queue to implement processes to capture pieces of a queue. The only problem: am I getting 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 fine too. ''' lim = len(queue) if len(queue) < lim else lim return [self.popleft() for i in range(lim)] 

testing this (I split it so that I no longer get involved)

 | => ./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:

+5
source share
1 answer

multiprocessing.Queue is a method that creates queues, so you should use it as a function my_queue = Queue() .

 >>> from multiprocessing import Queue >>> type(Queue) <class 'method'> 

As you can see, this is not a “type” that you would use for a subclass.

If you want to implement your own queue, you can take a look at queue.Queue

EDIT:

If you want to subclass a queue from multiprocessing, use multiprocessing.queues.Queue instead, which is the type of object returned by multiprocessing.Queue()

+5
source

Source: https://habr.com/ru/post/1238282/


All Articles