Title: Priority Queues
Slug: priority_queues
Summary: Priority Queues Using Python.
Date: 2017-02-02 12:00
Category: Python
Tags: Basics
Authors: Chris Albon

Preliminaries


In [1]:
import heapq

Create A Priority Queue Object


In [2]:
# Create a priority queue abstract base class
class priority_queue:
    # Initialize the instance
    def __init__(self):
        # Create a list to use as the queue
        self._queue = []
        # Create an index to use as ordering
        self._index = 0

    # Create a function to add a task to the queue
    def add_task(self, item, priority):
        # Push the arguments to the _queue using a heap
        heapq.heappush(self._queue, (-priority, self._index, item))
        # Add one to the index
        self._index += 1

    # Create a function to get the next item from the queue
    def next_task(self):
        # Return the next item in the queue
        return heapq.heappop(self._queue)[-1]

In [3]:
# Create a priority queue called task_list
task_list = priority_queue()

Add Items To Queue


In [4]:
# Add an item to the queue
task_list.add_task('Clean Dishes', 1)

# Add an item to the queue
task_list.add_task('Wash Car', 2)

# Add an item to the queue
task_list.add_task('Walk Dog', 3)

Retrieve Items From Queue By Priority


In [5]:
# Retrieve items from the queue
task_list.next_task()


Out[5]:
'Walk Dog'

In [6]:
# Retrieve items from the queue
task_list.next_task()


Out[6]:
'Wash Car'

In [7]:
# Retrieve items from the queue
task_list.next_task()


Out[7]:
'Clean Dishes'