我想子类化 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
编辑/更新:
multiprocessing.Queue
是一种创建队列的方法,因此您应该将其用作函数my_queue = Queue()
.
>>> from multiprocessing import Queue
>>> type(Queue)
<class 'method'>
正如你所看到的,这不是一个“类型”,你可以用它来子类化。
如果你想实现自己的队列,你可以看看队列.队列 https://docs.python.org/3.5/library/queue.html#queue.Queue
EDIT:
如果您想从多处理中子类化队列,请使用multiprocessing.queues.Queue
相反,它是返回的对象的类型multiprocessing.Queue()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)