Package grassyknoll :: Package lib :: Module ClosingQueue
[hide private]

Source Code for Module grassyknoll.lib.ClosingQueue

 1  # XXX this should probably be written by someone who knows WTF they're doing. 
 2   
 3  from Queue import Queue, Full, Empty 
 4   
 5  __all__=['ClosingQueue', 'Closed', 'Full', 'Empty'] 
 6   
7 -class Closed(Exception):
8 """exception raised when trying to put to a closed queue"""
9
10 -class ClosingQueue(Queue):
11 """a L{Queue} that can be L{close}d. 12 13 When the queue is closed, attempting to L{put} additional items will raise 14 L{Closed}. 15 16 A newly-created queue is open. 17 """ 18
19 - def _init(self, *args, **kwargs):
20 self._closed=False 21 Queue._init(self, *args, **kwargs)
22
23 - def closed(self):
24 """Return True if the queue is closed, False otherwise (not reliable!).""" 25 self.mutex.acquire() 26 n = self._closed 27 self.mutex.release() 28 return n
29
30 - def close(self):
31 """close the queue to new items""" 32 # XXX should this have block/timeout args? 33 self.mutex.acquire() 34 try: 35 self._closed=True 36 finally: 37 self.mutex.release()
38 39 # XXX this introduces weirdness if the box is full *and* closed, since it 40 # gets called after acquiring the condvar.
41 - def _put(self, item):
42 if self._closed: raise Closed 43 else: return Queue._put(self, item)
44 45 # XXX once the box is closed *and* empty, all further _get's should raise 46 # Closed. Nope, that won't work, b/c we may have threads blocked in get() 47 # before we become empty 48