Question regarding Queue object
Terry
terry.yinzhe at gmail.com
Tue Apr 29 10:36:59 EDT 2008
On Apr 29, 3:01 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
> On Sun, 27 Apr 2008 03:27:59 -0700 (PDT), Terry <terry.yin... at gmail.com>
> declaimed the following in comp.lang.python:
>
> > 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?
>
> Create your own queue class -- including locking objects.
>
> Implement the queue itself (I've not looked at how Queue.Queue is
> really done) as a priority queue (that is, a simple list ordered by your
> ID -- new items are inserted after all existing items with the same or
> lower ID number).
>
> Surround list manipulations with a lock based on a Condition.
>
> Now, the trick -- the .get(ID) sequence being something like (this
> is pseudo-code):
>
> while True:
> self.condition.acquire()
> scan self.qlist for first entry with ID
> if found:
> remove entry from self.qlist
> self.condition.release()
> return entry
> self.condition.wait()
>
> -=-=-=-=- the .put(ID, data) looks like
>
> self.condition.acquire()
> scan self.qlist for position to insert (ID, data)
> self.condition.notifyAll()
> self.condition.release()
>
> -=-=-=-=-
>
> Essentially, if the first pass over the list does not find an entry
> to return, it waits for a notify to occur... and notification will only
> occur when some other thread puts new data into the list.
> --
> Wulfraed Dennis Lee Bieber KD6MOG
> wlfr... at ix.netcom.com wulfr... at bestiaria.com
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff: web-a... at bestiaria.com)
> HTTP://www.bestiaria.com/
Yes, now I have a similar solution in my code. But after read the
stackless python, I'm thinking if I can move to stackless, which might
improve the performance of my thread. Because I'm trying to simulate
some behavior of the real world (trading), I believe there will be a
lot of threads in the future in my program.
More information about the Python-list
mailing list