多处理队列子类问题 [英] multiprocessing Queue subclass issue

查看:90
本文介绍了多处理队列子类问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想子类化 multiprocessing.Queue 以实现进程以获取队列的块.唯一的问题是,我收到了一个奇怪的 TypeError?

#!/usr/bin/env python#哇哦!?从多处理导入队列类BufferQueue(队列):'''一个线程/进程安全队列,用于带有导入的追加/弹出左操作缓冲.'''def __init__(self, **kwargs):super(BufferQueue,self).__init__(**kwargs)def 消耗(自我,lim):'''消耗最多但不超过 lim 元素并以新的方式返回它们列表,清理缓冲区.@参数lim -- 从列表中消耗的最大值(限制).如果项目少存在于列表中,那也没关系.'''lim = len(queue) 如果 len(queue) <其他 lim返回 [self.popleft() for i in range(lim)]

测试这个(我把它分开了,这样我就不会再拉其他东西了)

<代码>|=>./tests/wtf_queue.py回溯(最近一次调用最后一次):文件./tests/wtf_queue.py",第 10 行,在 <module> 中类BufferQueue(队列):类型错误:方法需要 2 个参数,得到 3 个

编辑/更新:

解决方案

multiprocessing.Queue 是一种创建队列的方法,因此您应该将其用作函数 my_queue =Queue().

<预><代码>>>>从多处理导入队列>>>类型(队列)<类'方法'>

正如您所见,它不是一个类型",您可以使用它来进行子类化.

如果你想实现自己的队列,你可以看看 queue.Queue

如果你想从 multiprocessing 子类化队列,使用 multiprocessing.queues.Queue 代替,它是 multiprocessing.Queue() 返回的对象类型/p>

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:

解决方案

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

这篇关于多处理队列子类问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆