简体   繁体   English

python中带有SyncManager类的多进程优先队列的队列函数

[英]queue function for Multiprocess Priority Queue in python with SyncManager class

I wanted to implement multiprocessing priorityqueue .我想实现多处理 priorityqueue 。 I found this answer :- Strange Queue.PriorityQueue behaviour with multiprocessing in Python 2.7.6我找到了这个答案:- 在 Python 2.7.6 中使用多处理的奇怪的 Queue.PriorityQueue 行为

by Dano达诺

After I implemented this .在我实施了这个之后。 I could use .get() and .put() function for my Priority Queue but when i used .queue to print the current elements in the queue it gave me an error我可以将 .get() 和 .put() 函数用于我的优先队列,但是当我使用 .queue 打印队列中的当前元素时,它给了我一个错误

code:-代码:-

 class MyManager(SyncManager):   
    pass

def get_manager():
    MyManager.register("PriorityQueue", PriorityQueue)  # Register a shared PriorityQueue
    m = MyManager()
    m.start()
    return m

m = get_manager()
call= m.PriorityQueue()
for i in range(5):
    call.put(i)

print(call.queue)

Error : AttributeError: 'AutoProxy[PriorityQueue]' object has no attribute 'queue'错误: AttributeError: 'AutoProxy[PriorityQueue]' object has no attribute 'queue'

I read the python documentation for SyncManager and modified my code .我阅读了 SyncManager 的 python 文档并修改了我的代码。

New code :-新代码:-

class MyManager(SyncManager):   
    pass

def get_manager():
    MyManager.register("PriorityQueue", PriorityQueue,exposed=['put','get','queue'])  # Register a shared PriorityQueue
    m = MyManager()
    m.start()
    return m

m = get_manager()
call= m.PriorityQueue()
for i in range(5):
    call.put(i)

print(call.queue)

Now output was :-现在输出是:-

<bound method AutoProxy[PriorityQueue].queue of <AutoProxy[PriorityQueue] object, typeid 'PriorityQueue' at 0x7ff3b48f2dd0>>

I am still not getting the elements in the queue , i read about method_to_typeid attribute of register function to map the return type of functions mentioned in exposed , but i don't know how use that .我仍然没有获取队列中的元素,我阅读了 register 函数的method_to_typeid属性以映射exposed提到的函数的返回类型,但我不知道如何使用它。

Can someone help me with this , so that i could print elements of the queue without poping them from queue有人可以帮我解决这个问题吗,这样我就可以打印队列的元素而不用从队列中弹出它们

You can only use methods of a referent through a proxy.您只能通过代理使用引用对象的方法 Since PriorityQueue().queue is not a method, but an instance attribute, you need to provide a method which can return the value of this attribute.由于PriorityQueue().queue不是一个方法,而是一个实例属性,所以需要提供一个可以返回该属性值的方法。 The example below opts for a generalized get_attribute method with subclassing PriorityQueue .下面的示例选择了具有子类PriorityQueue的通用get_attribute方法。

# Python 3.7.1
from queue import PriorityQueue
from multiprocessing.managers import SyncManager
from multiprocessing import Process


SENTINEL = None


class MyPriorityQueue(PriorityQueue):
    def get_attribute(self, name):
        return getattr(self, name)


class MyManager(SyncManager):
    pass


def get_manager():
    MyManager.register("PriorityQueue", MyPriorityQueue)
    m = MyManager()
    m.start()
    return m


def f(q):
    for item in iter(lambda: q.get()[1], SENTINEL):
        print(item)
    print(f'queue: {q.get_attribute("queue")}')


if __name__ == '__main__':

    m = get_manager()
    pq = m.PriorityQueue()

    tasks = enumerate([f'item_{i}' for i in range(5)] + [SENTINEL])

    for task in tasks:
        pq.put(task)

    print(f'queue: {pq.get_attribute("queue")}')
    print(f'maxsize: {pq.get_attribute("maxsize")}')

    p = Process(target=f, args=(pq,))
    p.start()
    p.join()

Example Output:示例输出:

queue: [(0, 'item_0'), (1, 'item_1'), (2, 'item_2'), (3, 'item_3'), (4, 'item_4'), (5, None)]
maxsize: 0
item_0
item_1
item_2
item_3
item_4
queue: []

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM