根据pyhton队列中的键值获取字典数据

2024-06-01 05:06:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在寻找一种队列机制解决方案,我们可以根据匹配键值从队列中获取特定的dict obj

目前在我的实现中:我已经定义了队列(FIFO),并根据下面的代码get_event_form_Queue()始终读取并删除FIFO顺序

此外,队列中存储的对象类型是dict,它有多个键值,其中一个键值对是'SEQ_NUM'=某个int值。因此,我想读取并删除与“SEQ_NUM”键值匹配的特定dict对象

class PyDriver:
    receive_events_queue = queue.Queue()

    ..................------
    -----------------------
    ------------------------
    @staticmethod
    def get_event_from_queue(timeout_in_sec):
        """get the events Queue
        return: events queue"""

        try:
            # blocking call ,wait until the event gets populated on the Q or timeout
            # raise the empty exception after the timeout
            error_code = 0
            the_event = PyDriver.receive_events_queue.get(True, timeout_in_sec)
            PyDriver.receive_events_queue.task_done()
        except queue.Empty as ex:
            the_event = None
            template = "An exception of type {0} occurred. Arguments:\n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)
        except Exception as ex:
            the_event = None
            template = "An exception of type {0} occurred. Arguments:\n{1!r}"
            message = template.format(type(ex).__name__, ex.args)
            print(message)

Tags: theeventmessageget队列queuetypetimeout