当前位置:  开发笔记 > 编程语言 > 正文

多处理队列子类问题

如何解决《多处理队列子类问题》经验,为你挑选了1个好方法。

我想将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 
    class BufferQueue(Queue):
TypeError: method expected 2 arguments, got 3

编辑/更新:



1> eugecm..:

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

>>> from multiprocessing import Queue
>>> type(Queue)

正如您所看到的,不是"类型",您将使用它来进行子类化.

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

编辑:

如果要从多处理中对队列进行子类化,请multiprocessing.queues.Queue改为使用,这是返回的对象的类型multiprocessing.Queue()


是的,文档需要对此做些什么.它在文档中特别称为类,此外该函数返回一个类.然后,最重要的是,queue.Queue不会被序列化,因此对多处理没有用.推入队列的进程与使用它的进程完全不同.
扩展multiprocessing.queues.Queue,然后在进程管理器上下文中实例化它似乎正在工作(queue = BufferQueue(ctx = get_context()))
推荐阅读
罗文彬2502852027
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有