home *** CD-ROM | disk | FTP | other *** search
/ PC Extra 07 & 08 / pca1507.iso / Software / psp8 / Data1.cab / Queue.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2003-04-22  |  5.6 KB  |  153 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.2)
  3.  
  4. '''A multi-producer, multi-consumer queue.'''
  5.  
  6. class Empty(Exception):
  7.     '''Exception raised by Queue.get(block=0)/get_nowait().'''
  8.     pass
  9.  
  10.  
  11. class Full(Exception):
  12.     '''Exception raised by Queue.put(block=0)/put_nowait().'''
  13.     pass
  14.  
  15.  
  16. class Queue:
  17.     
  18.     def __init__(self, maxsize = 0):
  19.         '''Initialize a queue object with a given maximum size.
  20.  
  21.         If maxsize is <= 0, the queue size is infinite.
  22.         '''
  23.         import thread
  24.         self._init(maxsize)
  25.         self.mutex = thread.allocate_lock()
  26.         self.esema = thread.allocate_lock()
  27.         self.esema.acquire()
  28.         self.fsema = thread.allocate_lock()
  29.  
  30.     
  31.     def qsize(self):
  32.         '''Return the approximate size of the queue (not reliable!).'''
  33.         self.mutex.acquire()
  34.         n = self._qsize()
  35.         self.mutex.release()
  36.         return n
  37.  
  38.     
  39.     def empty(self):
  40.         '''Return 1 if the queue is empty, 0 otherwise (not reliable!).'''
  41.         self.mutex.acquire()
  42.         n = self._empty()
  43.         self.mutex.release()
  44.         return n
  45.  
  46.     
  47.     def full(self):
  48.         '''Return 1 if the queue is full, 0 otherwise (not reliable!).'''
  49.         self.mutex.acquire()
  50.         n = self._full()
  51.         self.mutex.release()
  52.         return n
  53.  
  54.     
  55.     def put(self, item, block = 1):
  56.         """Put an item into the queue.
  57.  
  58.         If optional arg 'block' is 1 (the default), block if
  59.         necessary until a free slot is available.  Otherwise (block
  60.         is 0), put an item on the queue if a free slot is immediately
  61.         available, else raise the Full exception.
  62.         """
  63.         if block:
  64.             self.fsema.acquire()
  65.         elif not self.fsema.acquire(0):
  66.             raise Full
  67.         
  68.         self.mutex.acquire()
  69.         was_empty = self._empty()
  70.         self._put(item)
  71.         if was_empty:
  72.             self.esema.release()
  73.         
  74.         if not self._full():
  75.             self.fsema.release()
  76.         
  77.         self.mutex.release()
  78.  
  79.     
  80.     def put_nowait(self, item):
  81.         '''Put an item into the queue without blocking.
  82.  
  83.         Only enqueue the item if a free slot is immediately available.
  84.         Otherwise raise the Full exception.
  85.         '''
  86.         return self.put(item, 0)
  87.  
  88.     
  89.     def get(self, block = 1):
  90.         """Remove and return an item from the queue.
  91.  
  92.         If optional arg 'block' is 1 (the default), block if
  93.         necessary until an item is available.  Otherwise (block is 0),
  94.         return an item if one is immediately available, else raise the
  95.         Empty exception.
  96.         """
  97.         if block:
  98.             self.esema.acquire()
  99.         elif not self.esema.acquire(0):
  100.             raise Empty
  101.         
  102.         self.mutex.acquire()
  103.         was_full = self._full()
  104.         item = self._get()
  105.         if was_full:
  106.             self.fsema.release()
  107.         
  108.         if not self._empty():
  109.             self.esema.release()
  110.         
  111.         self.mutex.release()
  112.         return item
  113.  
  114.     
  115.     def get_nowait(self):
  116.         '''Remove and return an item from the queue without blocking.
  117.  
  118.         Only get an item if one is immediately available.  Otherwise
  119.         raise the Empty exception.
  120.         '''
  121.         return self.get(0)
  122.  
  123.     
  124.     def _init(self, maxsize):
  125.         self.maxsize = maxsize
  126.         self.queue = []
  127.  
  128.     
  129.     def _qsize(self):
  130.         return len(self.queue)
  131.  
  132.     
  133.     def _empty(self):
  134.         return not (self.queue)
  135.  
  136.     
  137.     def _full(self):
  138.         if self.maxsize > 0:
  139.             pass
  140.         return len(self.queue) == self.maxsize
  141.  
  142.     
  143.     def _put(self, item):
  144.         self.queue.append(item)
  145.  
  146.     
  147.     def _get(self):
  148.         item = self.queue[0]
  149.         del self.queue[0]
  150.         return item
  151.  
  152.  
  153.