<s> """<STR_LIT>""" <EOL> from time import time as _time <EOL> try : <EOL> import threading as _threading <EOL> except ImportError : <EOL> import dummy_threading as _threading <EOL> from collections import deque <EOL> import heapq <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class Empty ( Exception ) : <EOL> "<STR_LIT>" <EOL> pass <EOL> class Full ( Exception ) : <EOL> "<STR_LIT>" <EOL> pass <EOL> class Queue : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , maxsize = <NUM_LIT:0> ) : <EOL> self . maxsize = maxsize <EOL> self . _init ( maxsize ) <EOL> self . mutex = _threading . Lock ( ) <EOL> self . not_empty = _threading . Condition ( self . mutex ) <EOL> self . not_full = _threading . Condition ( self . mutex ) <EOL> self . all_tasks_done = _threading . Condition ( self . mutex ) <EOL> self . unfinished_tasks = <NUM_LIT:0> <EOL> def task_done ( self ) : <EOL> """<STR_LIT>""" <EOL> self . all_tasks_done . acquire ( ) <EOL> try : <EOL> unfinished = self . unfinished_tasks - <NUM_LIT:1> <EOL> if unfinished <= <NUM_LIT:0> : <EOL> if unfinished < <NUM_LIT:0> : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> self . all_tasks_done . notify_all ( ) <EOL> self . unfinished_tasks = unfinished <EOL> finally : <EOL> self . all_tasks_done . release ( ) <EOL> def join ( self ) : <EOL> """<STR_LIT>""" <EOL> self . all_tasks_done . acquire ( ) <EOL> try : <EOL> while self . unfinished_tasks : <EOL> self . all_tasks_done . wait ( ) <EOL> finally : <EOL> self . all_tasks_done . release ( ) <EOL> def qsize ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mutex . acquire ( ) <EOL> n = self . _qsize ( ) <EOL> self . mutex . release ( ) <EOL> return n <EOL> def empty ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mutex . acquire ( ) <EOL> n = not self . _qsize ( ) <EOL> self . mutex . release ( ) <EOL> return n <EOL> def full ( self ) : <EOL> """<STR_LIT>""" <EOL> self . mutex . acquire ( ) <EOL> n = <NUM_LIT:0> < self . maxsize == self . _qsize ( ) <EOL> self . mutex . release ( ) <EOL> return n <EOL> def put ( self , item , block = True , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> self . not_full . acquire ( ) <EOL> try : <EOL> if self . maxsize > <NUM_LIT:0> : <EOL> if not block : <EOL> if self . _qsize ( ) == self . maxsize : <EOL> raise Full <EOL> elif timeout is None : <EOL> while self . _qsize ( ) == self . maxsize : <EOL> self . not_full . wait ( ) <EOL> elif timeout < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> endtime = _time ( ) + timeout <EOL> while self . _qsize ( ) == self . maxsize : <EOL> remaining = endtime - _time ( ) <EOL> if remaining <= <NUM_LIT:0.0> : <EOL> raise Full <EOL> self . not_full . wait ( remaining ) <EOL> self . _put ( item ) <EOL> self . unfinished_tasks += <NUM_LIT:1> <EOL> self . not_empty . notify ( ) <EOL> finally : <EOL> self . not_full . release ( ) <EOL> def put_nowait ( self , item ) : <EOL> """<STR_LIT>""" <EOL> return self . put ( item , False ) <EOL> def get ( self , block = True , timeout = None ) : <EOL> """<STR_LIT>""" <EOL> self . not_empty . acquire ( ) <EOL> try : <EOL> if not block : <EOL> if not self . _qsize ( ) : <EOL> raise Empty <EOL> elif timeout is None : <EOL> while not self . _qsize ( ) : <EOL> self . not_empty . wait ( ) <EOL> elif timeout < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> endtime = _time ( ) + timeout <EOL> while not self . _qsize ( ) : <EOL> remaining = endtime - _time ( ) <EOL> if remaining <= <NUM_LIT:0.0> : <EOL> raise Empty <EOL> self . not_empty . wait ( remaining ) <EOL> item = self . _get ( ) <EOL> self . not_full . notify ( ) <EOL> return item <EOL> finally : <EOL> self . not_empty . release ( ) <EOL> def get_nowait ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . get ( False ) <EOL> def _init ( self , maxsize ) : <EOL> self . queue = deque ( ) <EOL> def _qsize ( self , len = len ) : <EOL> return len ( self . queue ) <EOL> def _put ( self , item ) : <EOL> self . queue . append ( item ) <EOL> def _get ( self ) : <EOL> return self . queue . popleft ( ) <EOL> class PriorityQueue ( Queue ) : <EOL> '''<STR_LIT>''' <EOL> def _init ( self , maxsize ) : <EOL> self . queue = [ ] <EOL> def _qsize ( self , len = len ) : <EOL> return len ( self . queue ) <EOL> def _put ( self , item , heappush = heapq . heappush ) : <EOL> heappush ( self . queue , item ) <EOL> def _get ( self , heappop = heapq . heappop ) : <EOL> return heappop ( self . queue ) <EOL> class LifoQueue ( Queue ) : <EOL> '''<STR_LIT>''' <EOL> def _init ( self , maxsize ) : <EOL> self . queue = [ ] <EOL> def _qsize ( self , len = len ) : <EOL> return len ( self . queue ) <EOL> def _put ( self , item ) : <EOL> self . queue . append ( item ) <EOL> def _get ( self ) : <EOL> return self . queue . pop ( ) </s>
