home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / Chip_2003-01_cd2.bin / convert / eJayMp3Pro / mp3pro_demo.exe / QUEUE.PYC (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2000-06-16  |  5.6 KB  |  156 lines

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