Question regarding Queue object
terry.yinzhe at gmail.com
Sun Apr 27 12:27:59 CEST 2008
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 = 
start = time.clock()
msg =self.q.get(wait, to)
if msg and msg['id'] in ids:
# not the expecting message, save it.
to = to - (time.clock()-start)
if to <= 0:
# put the saved messages back to the queue
for m in saved:
More information about the Python-list