2015-12-15 60 views
5

我想子類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 

編輯/更新:

+0

你如何初始化隊列? – eugecm

+0

我不是。你看到的是整個測試。我實際上並沒有以任何方式打電話或使用它。 – SkyLeach

+0

我在想這與multiprocessing.Queue處理本地/共享資源的方式有關。在JIT加載過程中將類規範稱爲TypeDef意味着核心中的某些內容正在變得越來越大AFAICT – SkyLeach

回答

5

multiprocessing.Queue是創建隊列的方法,讓你應該用它作爲函數my_queue = Queue()

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

正如你所看到的不是一個'類型',你會用它來進行子類化。

如果你想實現自己的隊列中,你可以看看queue.Queue

編輯:

如果你想繼承從多隊列,使用multiprocessing.queues.Queue代替,這是該類型由multiprocessing.Queue()返回的對象

+1

是的,文檔需要對此做些事情。它在文檔中專門稱爲類,此外該函數還會返回一個類。然後,最重要的是,queue.Queue不會被序列化,因此對多處理沒有用處。推入隊列的進程與使用它的進程完全不同。 – SkyLeach

+0

現在測試它,看看它是否工作或休息。我懷疑這樣做會起作用。 – SkyLeach

+0

擴展multiprocessing.queues.Queue,然後在進程管理器上下文中實例化它似乎正在工作(queue = BufferQueue(ctx = get_context())) – SkyLeach