Package qm :: Module queue
[hide private]
[frames] | no frames]

Source Code for Module qm.queue

  1  ######################################################################## 
  2  # 
  3  # File:   queue.py 
  4  # Author: Mark Mitchell 
  5  # Date:   01/07/2002 
  6  # 
  7  # Contents: 
  8  #   Queue 
  9  # 
 10  # Copyright (c) 2002 by CodeSourcery, LLC.  All rights reserved.  
 11  # 
 12  # For license terms see the file COPYING. 
 13  # 
 14  ######################################################################## 
 15   
 16  ######################################################################## 
 17  # Notes 
 18  ######################################################################## 
 19   
 20  # On systems that do not support threads, the Python Queue module 
 21  # does not work.  This is probably a bug in Python; if there are 
 22  # no threads, there is no difficulty in being threadsafe, and the 
 23  # module should simply omit the calls to acquire and release locks. 
 24  # This module is a manual implementation of this idea, with the 
 25  # limitation that the maxsize parameter to the initialization function 
 26  # must always be zero. 
 27   
 28  try: 
 29      import thread 
 30   
 31      # If we successfully imported the thread module, we can just use 
 32      # the builtin Queue. 
 33      from Queue import * 
 34  except: 
 35      # This code is based on the Python 2.2 Queue.py, but without 
 36      # the threading calls. 
37 - class Empty(Exception):
38 "Exception raised by Queue.get(block=0)/get_nowait()." 39 pass
40
41 - class Queue:
42 - def __init__(self, maxsize=0):
43 """Initialize a queue object with a given maximum size. 44 45 If maxsize is <= 0, the queue size is infinite. 46 """ 47 assert maxsize <= 0 48 self._init(maxsize)
49
50 - def qsize(self):
51 """Return the approximate size of the queue (not reliable!).""" 52 n = self._qsize() 53 return n
54
55 - def empty(self):
56 """Return 1 if the queue is empty, 0 otherwise (not reliable!).""" 57 n = self._empty() 58 return n
59
60 - def full(self):
61 """Return 1 if the queue is full, 0 otherwise (not reliable!).""" 62 n = self._full() 63 return n
64
65 - def put(self, item, block=1):
66 """Put an item into the queue. 67 68 If optional arg 'block' is 1 (the default), block if 69 necessary until a free slot is available. Otherwise (block 70 is 0), put an item on the queue if a free slot is immediately 71 available, else raise the Full exception. 72 """ 73 self._put(item)
74
75 - def put_nowait(self, item):
76 """Put an item into the queue without blocking. 77 78 Only enqueue the item if a free slot is immediately available. 79 Otherwise raise the Full exception. 80 """ 81 return self.put(item, 0)
82
83 - def get(self, block=1):
84 """Remove and return an item from the queue. 85 86 If optional arg 'block' is 1 (the default), block if 87 necessary until an item is available. Otherwise (block is 0), 88 return an item if one is immediately available, else raise the 89 Empty exception. 90 """ 91 if not block and not self.queue: 92 raise Empty 93 item = self._get() 94 return item
95
96 - def get_nowait(self):
97 """Remove and return an item from the queue without blocking. 98 99 Only get an item if one is immediately available. Otherwise 100 raise the Empty exception. 101 """ 102 return self.get(0)
103 104 # Override these methods to implement other queue organizations 105 # (e.g. stack or priority queue). 106 # These will only be called with appropriate locks held 107 108 # Initialize the queue representation
109 - def _init(self, maxsize):
110 self.maxsize = maxsize 111 self.queue = []
112
113 - def _qsize(self):
114 return len(self.queue)
115 116 # Check whether the queue is empty
117 - def _empty(self):
118 return not self.queue
119 120 # Check whether the queue is full
121 - def _full(self):
122 return self.maxsize > 0 and len(self.queue) == self.maxsize
123 124 # Put a new item in the queue
125 - def _put(self, item):
126 self.queue.append(item)
127 128 # Get an item from the queue
129 - def _get(self):
130 item = self.queue[0] 131 del self.queue[0] 132 return item
133