msikora
msikora

Reputation: 35

Issues using Queues in python when getting objects to skip the queue

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.

Upvotes: 0

Views: 834

Answers (1)

gak
gak

Reputation: 32773

Have you looked at PriorityQueue?

Upvotes: 2

Related Questions