多处理队列子类问题
multiprocessing Queue subclass issue
我想子类化 multiprocessing.Queue 以实现进程以获取队列块。唯一的问题是,我遇到了一个奇怪的 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)]
测试这个(我把它分开了,这样我就不会引入任何其他东西)
| => ./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:
multiprocessing.Queue
是一种创建队列的方法,因此您应该将其用作函数 my_queue = Queue()
.
>>> from multiprocessing import Queue
>>> type(Queue)
<class 'method'>
如您所见,这不是您将用于子类化的 'type'。
如果你想实现自己的队列,你可以看看queue.Queue
编辑:
如果您想从多处理中继承队列,请改用multiprocessing.queues.Queue
,这是multiprocessing.Queue()
返回的对象类型
我想子类化 multiprocessing.Queue 以实现进程以获取队列块。唯一的问题是,我遇到了一个奇怪的 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)]
测试这个(我把它分开了,这样我就不会引入任何其他东西)
| => ./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:
multiprocessing.Queue
是一种创建队列的方法,因此您应该将其用作函数 my_queue = Queue()
.
>>> from multiprocessing import Queue
>>> type(Queue)
<class 'method'>
如您所见,这不是您将用于子类化的 'type'。
如果你想实现自己的队列,你可以看看queue.Queue
编辑:
如果您想从多处理中继承队列,请改用multiprocessing.queues.Queue
,这是multiprocessing.Queue()