priority_queue

This module provides queue containers for scheduler requests for the Robotics in Concert (ROCON) project.

class concert_scheduler_requests.priority_queue.PriorityQueue(iterable=[])[source]

This is a container class for ROCON scheduler request queue elements.

Parameters:iterable – Iterable yielding initial contents, either QueueElement objects, or something that behaves similarly.

This implementation is based on the heapq module and uses some of the ideas explained in its priority queue implementation notes.

len(queue)
Returns :The number of elements in the queue.
request in queue
Param request:(uuid.UUID or QueueElement) request to query.
Returns :True if request is in the queue.
add(element, priority=None)[source]

Add a new element to the queue.

Parameters:
  • element (QueueElement) – Queue element to add.
  • priority (int) – (Optional) new priority for this element.

If a request with the same identifier was already in the queue, its old element is removed and replaced by the new element.

If a new priority is specified, the priority of the original request is updated. That is the only safe way to change the priority of an element that is already queued.

Warning

Changing priority via some other name for that request would break the queue implementation.

peek()[source]

Return the top-priority element from the queue head without removing it.

Raises :IndexError if queue was empty.
pop()[source]

Remove the top-priority element from the queue head.

Raises :IndexError if queue was empty.
remove(request_id)[source]

Remove element corresponding to request_id.

Parameters:request_id (uuid.UUID or QueueElement) – Identifier of the request to remove.
Raises :KeyError if request_id not in the queue.
values()[source]

Current queue contents. :returns: iterable with active queue elements in random order.

class concert_scheduler_requests.priority_queue.QueueElement(request, requester_id)[source]

Request queue element class.

Parameters:
  • request (ActiveRequest) – Corresponding scheduler request object.
  • requester_id (uuid.UUID) – Unique identifier of requester.

Queue elements need fit into normal Python dictionaries, so they provide the required hash() operator, based on the unique ID of that request.

hash(element)
Returns :(int) Hash signature for element.

Python 3 requires that hashable objects must also provide an equals operator. The hash signatures of equal requests must be equal.

element == other
Returns :True if element and other have the same request ID (not their requester_id values).
element != other
Returns :True if element and other do not have the same request ID.

Queue elements need to sort in the normal Python way, so they provide the required < operator. The __cmp__ method is not used, because Python 3 does not allow it. But, we want requests with higher-numbered priorities to sort ahead of lower priority ones, so heapq and other Python modules work properly.

element < other
Returns :True if element has higher priority than other, or their priorities are the same and element has a lower sequence number.

This class does not provide a total ordering. The == and < operators test completely different fields. However, the request identifiers are unique, so no two valid queue elements should ever compare both equal and less, although that situation could be constructed artificially.

str(element)
Returns :String representation of this queue element, empty if it is not active.
request = None

Corresponding scheduler ActiveRequest object.

requester_id = None

uuid.UUID of requester.

sequence = None

Unique sequence number of this queue element. All elements created earlier have lower numbers, those created afterward will be higher.

active = None

True unless this element has been removed from its queue.

Previous topic

exceptions

Next topic

requester

This Page