'''
The order of items in both the FIFO Queue and LIFO Queue are related to the order of insertion.
However, there are many cases, the order of items in a queue are determined based on their importance or priority.

 For instance, to develop a task scheduler,

one common design is to server the most agent task in the queue.
 PriorityQueue is such a data structure that can be used pick up the items from the queue based on their priority value.

The constructor of python PriorityQueue is as follows:

class Queue.PriorityQueue(maxsize=0)
maxsize is an integer used to limit the items that can be placed in the queue.
Insertion will block once queue is full, until queue items are consumed . The queue size is infinite if the maxsize is negative or zero.

Please note that:
The lowest valued entries are retrieved first. A typical pattern for entries is a tuple in this form: (the_priority_number, data_value).

In the following example, we use Python PriorityQueue to simulate a task scheduler.  You can see that after put the tasks into the queue, the tasks are removed from the queue based on their priority score. Please note that: an item with the lowest value of the priority score will be removed first from the queue


http://www.learn4master.com/programming-language/python/python-queue-for-multithreading

'''

import Queue

class Task(object):
    def __init__(self, priority, name):
        self.priority = priority
        self.name = name

''' overriding cmp will order the queue from high to low , default all queue is low to high'''
     def __cmp__(self, other):
        return cmp(self.priority, other.priority)

q = Queue.PriorityQueue()

q.put( Task(100, 'a not agent task') )
q.put( Task(5, 'a highly agent task') )
q.put( Task(10, 'an important task') )

while not q.empty():
    cur_task = q.get()
    print 'process task:', cur_task.name
'''
#the output
process task: a highly agent task
process task: an important task
process task: a not agent task
'''
