Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Right now I am implementing a thread that will basically act as a consumer. There will be a FIFO queue that will get filled by other threads. The problem is that I want to add a special condition that will cause an item to skip the queue entirely.

def EventThread (threading.Thread):
    def __init__ (self, counter_object):
        super (EventThread, self).__init__ ()
        self.queue = Queue.Queue ()
        self.priority = threading.Event ()
        self.item = None
        self.counter = counter.object

    def add_to_queue (self, item):
        self.queue.put (item)

    def do_priority (self, item)
        self.item = item
        self.priority.set ()
        # do something here to block until priority goes back

    def run (self):
        while True:
            # Wait for open event slots
            if not self.counter.has_slots ():
                time.sleep (self.counter.next_slot ())

            # Block until queue has something
            item = self.queue.get (True)

            # If priority is set we need to do that first
            if self.priority.is_set ():
                 do_something_special (self.item)
                 self.priority.clear ()
            else:
                 do_something (item)

            # first if priority was set we lost the object
            # second the thread blocks until the queue has an object, doesn't
            # metter if priority has been set

This is just some pseudo code to detail my point. But as you can see there are some big problems there. There is no need to worry about the counter only this thread accesses it.

What I really need here is some way to get the thread to block until either the priority item switches or the queue gets an item. I am rather new to multi threading so I am not aware of anyone object that fit my needs or any trick to pull this off.

Many thanks to anyone who can help.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Have you looked at PriorityQueue?

share|improve this answer
    
That seems to do part of what I need, the description was rather vague so I skimmed over it first. But I sill need the do_priority function to block until that first item has been taken of the list. –  Flarkis Jan 24 '11 at 20:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.