Question regarding Queue object

Terry terry.yinzhe at gmail.com
Sun Apr 27 12:27:59 CEST 2008


Hello!

I'm trying to implement a message queue among threads using Queue. The
message queue has two operations:
PutMsg(id, msg) #  this is simple, just combine the id and msg as one
and put it into the Queue.
WaitMsg(ids, msg) # this is the hard part

WaitMsg will get only msg with certain ids, but this is not possible
in Queue object, because Queue provides no method to peek into the
message queue and fetch only matched item.

Now I'm using an ugly solution, fetch all the messages and put the not
used ones back to the queue. But I want a better performance. Is there
any alternative out there?

This is my current solution:

    def _get_with_ids(self,wait, timeout, ids):
        to = timeout
        msg = None
        saved = []
        while True:
            start = time.clock()
            msg =self.q.get(wait, to)
            if msg and msg['id'] in ids:
                break;
            # not the expecting message, save it.
            saved.append(msg)
            to = to - (time.clock()-start)
            if to <= 0:
                break
        # put the saved messages back to the queue
        for m in saved:
            self.q.put(m, True)
        return msg

br, Terry



More information about the Python-list mailing list