[code.view]

[top] / python / PyMOTW / Queue / Queue_priority.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """PriorityQueue
     """
     #end_pymotw_header
     
     import Queue
     
     class Job(object):
         def __init__(self, priority, description):
             self.priority = priority
             self.description = description
             print 'New job:', description
             return
         def __cmp__(self, other):
             return cmp(self.priority, other.priority)
     
     q = Queue.PriorityQueue()
     
     q.put( Job(3, 'Mid-level job') )
     q.put( Job(10, 'Low-level job') )
     q.put( Job(1, 'Important job') )
     
     while not q.empty():
         next_job = q.get()
         print 'Processing job:', next_job.description
         
     

[top] / python / PyMOTW / Queue / Queue_priority.py

contact | logmethods.com