Python 3 priority queues – source code examples

In the post FIFO and LIFO queues in python I shown how to use queues in python 3.
Python provides also priority queues.

You can use such a queue when the processing order of the items in a queue needs to be based on the importance of the items.
In a priority queue, the most important element will always be popped first, so in a priority queue you have to push a tuple of at least two elements: the priority and the content. The element with the lowest value for the priority will be popped.

The following program populates a priority queue with random priority values and some text.

then it pops data from the priority queue and prints out the popped items

You can find the full source code with the latest updates on my github: prio_queue.py

When you run the prio_queue.py program it will ouput something like the following

as you can see, it pushes data with random priority. And, then:

it pops data starting with lowest priorities.

Gg1

Related Posts

1 thought on “Python 3 priority queues – source code examples

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.