<s> """<STR_LIT>""" <EOL> import sys as _sys <EOL> try : <EOL> import thread <EOL> except ImportError : <EOL> del _sys . modules [ __name__ ] <EOL> raise <EOL> import warnings <EOL> from functools import wraps <EOL> from time import time as _time , sleep as _sleep <EOL> from traceback import format_exc as _format_exc <EOL> from collections import deque <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> _start_new_thread = thread . start_new_thread <EOL> _allocate_lock = thread . allocate_lock <EOL> _get_ident = thread . get_ident <EOL> ThreadError = thread . error <EOL> del thread <EOL> warnings . filterwarnings ( '<STR_LIT:ignore>' , category = DeprecationWarning , <EOL> module = '<STR_LIT>' , message = '<STR_LIT>' ) <EOL> _VERBOSE = False <EOL> if __debug__ : <EOL> class _Verbose ( object ) : <EOL> def __init__ ( self , verbose = None ) : <EOL> if verbose is None : <EOL> verbose = _VERBOSE <EOL> self . __verbose = verbose <EOL> def _note ( self , format , * args ) : <EOL> if self . __verbose : <EOL> format = format % args <EOL> format = "<STR_LIT>" % ( <EOL> current_thread ( ) . name , format ) <EOL> _sys . stderr . write ( format ) <EOL> else : <EOL> class _Verbose ( object ) : <EOL> def __init__ ( self , verbose = None ) : <EOL> pass <EOL> def _note ( self , * args ) : <EOL> pass <EOL> _profile_hook = None <EOL> _trace_hook = None <EOL> def setprofile ( func ) : <EOL> global _profile_hook <EOL> _profile_hook = func <EOL> def settrace ( func ) : <EOL> global _trace_hook <EOL> _trace_hook = func <EOL> Lock = _allocate_lock <EOL> def RLock ( * args , ** kwargs ) : <EOL> return _RLock ( * args , ** kwargs ) <EOL> class _RLock ( _Verbose ) : <EOL> def __init__ ( self , verbose = None ) : <EOL> _Verbose . __init__ ( self , verbose ) <EOL> self . __block = _allocate_lock ( ) <EOL> self . __owner = None <EOL> self . __count = <NUM_LIT:0> <EOL> def __repr__ ( self ) : <EOL> owner = self . __owner <EOL> return "<STR_LIT>" % ( <EOL> self . __class__ . __name__ , <EOL> owner and owner . name , <EOL> self . __count ) <EOL> def acquire ( self , blocking = <NUM_LIT:1> ) : <EOL> me = current_thread ( ) <EOL> if self . __owner is me : <EOL> self . __count = self . __count + <NUM_LIT:1> <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self , blocking ) <EOL> return <NUM_LIT:1> <EOL> rc = self . __block . acquire ( blocking ) <EOL> if rc : <EOL> self . __owner = me <EOL> self . __count = <NUM_LIT:1> <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self , blocking ) <EOL> else : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self , blocking ) <EOL> return rc <EOL> __enter__ = acquire <EOL> def release ( self ) : <EOL> if self . __owner is not current_thread ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> self . __count = count = self . __count - <NUM_LIT:1> <EOL> if not count : <EOL> self . __owner = None <EOL> self . __block . release ( ) <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> else : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> def __exit__ ( self , t , v , tb ) : <EOL> self . release ( ) <EOL> def _acquire_restore ( self , count_owner ) : <EOL> count , owner = count_owner <EOL> self . __block . acquire ( ) <EOL> self . __count = count <EOL> self . __owner = owner <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> def _release_save ( self ) : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> count = self . __count <EOL> self . __count = <NUM_LIT:0> <EOL> owner = self . __owner <EOL> self . __owner = None <EOL> self . __block . release ( ) <EOL> return ( count , owner ) <EOL> def _is_owned ( self ) : <EOL> return self . __owner is current_thread ( ) <EOL> def Condition ( * args , ** kwargs ) : <EOL> return _Condition ( * args , ** kwargs ) <EOL> class _Condition ( _Verbose ) : <EOL> def __init__ ( self , lock = None , verbose = None ) : <EOL> _Verbose . __init__ ( self , verbose ) <EOL> if lock is None : <EOL> lock = RLock ( ) <EOL> self . __lock = lock <EOL> self . acquire = lock . acquire <EOL> self . release = lock . release <EOL> try : <EOL> self . _release_save = lock . _release_save <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> self . _acquire_restore = lock . _acquire_restore <EOL> except AttributeError : <EOL> pass <EOL> try : <EOL> self . _is_owned = lock . _is_owned <EOL> except AttributeError : <EOL> pass <EOL> self . __waiters = [ ] <EOL> def __enter__ ( self ) : <EOL> return self . __lock . __enter__ ( ) <EOL> def __exit__ ( self , * args ) : <EOL> return self . __lock . __exit__ ( * args ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __lock , len ( self . __waiters ) ) <EOL> def _release_save ( self ) : <EOL> self . __lock . release ( ) <EOL> def _acquire_restore ( self , x ) : <EOL> self . __lock . acquire ( ) <EOL> def _is_owned ( self ) : <EOL> if self . __lock . acquire ( <NUM_LIT:0> ) : <EOL> self . __lock . release ( ) <EOL> return False <EOL> else : <EOL> return True <EOL> def wait ( self , timeout = None ) : <EOL> if not self . _is_owned ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> waiter = _allocate_lock ( ) <EOL> waiter . acquire ( ) <EOL> self . __waiters . append ( waiter ) <EOL> saved_state = self . _release_save ( ) <EOL> try : <EOL> if timeout is None : <EOL> waiter . acquire ( ) <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> else : <EOL> endtime = _time ( ) + timeout <EOL> delay = <NUM_LIT> <EOL> while True : <EOL> gotit = waiter . acquire ( <NUM_LIT:0> ) <EOL> if gotit : <EOL> break <EOL> remaining = endtime - _time ( ) <EOL> if remaining <= <NUM_LIT:0> : <EOL> break <EOL> delay = min ( delay * <NUM_LIT:2> , remaining , <NUM_LIT> ) <EOL> _sleep ( delay ) <EOL> if not gotit : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self , timeout ) <EOL> try : <EOL> self . __waiters . remove ( waiter ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self , timeout ) <EOL> finally : <EOL> self . _acquire_restore ( saved_state ) <EOL> def notify ( self , n = <NUM_LIT:1> ) : <EOL> if not self . _is_owned ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> __waiters = self . __waiters <EOL> waiters = __waiters [ : n ] <EOL> if not waiters : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> return <EOL> self . _note ( "<STR_LIT>" , self , n , <EOL> n != <NUM_LIT:1> and "<STR_LIT:s>" or "<STR_LIT>" ) <EOL> for waiter in waiters : <EOL> waiter . release ( ) <EOL> try : <EOL> __waiters . remove ( waiter ) <EOL> except ValueError : <EOL> pass <EOL> def notifyAll ( self ) : <EOL> self . notify ( len ( self . __waiters ) ) <EOL> notify_all = notifyAll <EOL> def Semaphore ( * args , ** kwargs ) : <EOL> return _Semaphore ( * args , ** kwargs ) <EOL> class _Semaphore ( _Verbose ) : <EOL> def __init__ ( self , value = <NUM_LIT:1> , verbose = None ) : <EOL> if value < <NUM_LIT:0> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> _Verbose . __init__ ( self , verbose ) <EOL> self . __cond = Condition ( Lock ( ) ) <EOL> self . __value = value <EOL> def acquire ( self , blocking = <NUM_LIT:1> ) : <EOL> rc = False <EOL> self . __cond . acquire ( ) <EOL> while self . __value == <NUM_LIT:0> : <EOL> if not blocking : <EOL> break <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , <EOL> self , blocking , self . __value ) <EOL> self . __cond . wait ( ) <EOL> else : <EOL> self . __value = self . __value - <NUM_LIT:1> <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , <EOL> self , self . __value ) <EOL> rc = True <EOL> self . __cond . release ( ) <EOL> return rc <EOL> __enter__ = acquire <EOL> def release ( self ) : <EOL> self . __cond . acquire ( ) <EOL> self . __value = self . __value + <NUM_LIT:1> <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , <EOL> self , self . __value ) <EOL> self . __cond . notify ( ) <EOL> self . __cond . release ( ) <EOL> def __exit__ ( self , t , v , tb ) : <EOL> self . release ( ) <EOL> def BoundedSemaphore ( * args , ** kwargs ) : <EOL> return _BoundedSemaphore ( * args , ** kwargs ) <EOL> class _BoundedSemaphore ( _Semaphore ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value = <NUM_LIT:1> , verbose = None ) : <EOL> _Semaphore . __init__ ( self , value , verbose ) <EOL> self . _initial_value = value <EOL> def release ( self ) : <EOL> if self . _Semaphore__value >= self . _initial_value : <EOL> raise ValueError , "<STR_LIT>" <EOL> return _Semaphore . release ( self ) <EOL> def Event ( * args , ** kwargs ) : <EOL> return _Event ( * args , ** kwargs ) <EOL> class _Event ( _Verbose ) : <EOL> def __init__ ( self , verbose = None ) : <EOL> _Verbose . __init__ ( self , verbose ) <EOL> self . __cond = Condition ( Lock ( ) ) <EOL> self . __flag = False <EOL> def isSet ( self ) : <EOL> return self . __flag <EOL> is_set = isSet <EOL> def set ( self ) : <EOL> self . __cond . acquire ( ) <EOL> try : <EOL> self . __flag = True <EOL> self . __cond . notify_all ( ) <EOL> finally : <EOL> self . __cond . release ( ) <EOL> def clear ( self ) : <EOL> self . __cond . acquire ( ) <EOL> try : <EOL> self . __flag = False <EOL> finally : <EOL> self . __cond . release ( ) <EOL> def wait ( self , timeout = None ) : <EOL> self . __cond . acquire ( ) <EOL> try : <EOL> if not self . __flag : <EOL> self . __cond . wait ( timeout ) <EOL> finally : <EOL> self . __cond . release ( ) <EOL> _counter = <NUM_LIT:0> <EOL> def _newname ( template = "<STR_LIT>" ) : <EOL> global _counter <EOL> _counter = _counter + <NUM_LIT:1> <EOL> return template % _counter <EOL> _active_limbo_lock = _allocate_lock ( ) <EOL> _active = { } <EOL> _limbo = { } <EOL> class Thread ( _Verbose ) : <EOL> __initialized = False <EOL> __exc_info = _sys . exc_info <EOL> __exc_clear = _sys . exc_clear <EOL> def __init__ ( self , group = None , target = None , name = None , <EOL> args = ( ) , kwargs = None , verbose = None ) : <EOL> assert group is None , "<STR_LIT>" <EOL> _Verbose . __init__ ( self , verbose ) <EOL> if kwargs is None : <EOL> kwargs = { } <EOL> self . __target = target <EOL> self . __name = str ( name or _newname ( ) ) <EOL> self . __args = args <EOL> self . __kwargs = kwargs <EOL> self . __daemonic = self . _set_daemon ( ) <EOL> self . __ident = None <EOL> self . __started = Event ( ) <EOL> self . __stopped = False <EOL> self . __block = Condition ( Lock ( ) ) <EOL> self . __initialized = True <EOL> self . __stderr = _sys . stderr <EOL> def _set_daemon ( self ) : <EOL> return current_thread ( ) . daemon <EOL> def __repr__ ( self ) : <EOL> assert self . __initialized , "<STR_LIT>" <EOL> status = "<STR_LIT>" <EOL> if self . __started . is_set ( ) : <EOL> status = "<STR_LIT>" <EOL> if self . __stopped : <EOL> status = "<STR_LIT>" <EOL> if self . __daemonic : <EOL> status += "<STR_LIT>" <EOL> if self . __ident is not None : <EOL> status += "<STR_LIT>" % self . __ident <EOL> return "<STR_LIT>" % ( self . __class__ . __name__ , self . __name , status ) <EOL> def start ( self ) : <EOL> if not self . __initialized : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if self . __started . is_set ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> _active_limbo_lock . acquire ( ) <EOL> _limbo [ self ] = self <EOL> _active_limbo_lock . release ( ) <EOL> _start_new_thread ( self . __bootstrap , ( ) ) <EOL> self . __started . wait ( ) <EOL> def run ( self ) : <EOL> try : <EOL> if self . __target : <EOL> self . __target ( * self . __args , ** self . __kwargs ) <EOL> finally : <EOL> del self . __target , self . __args , self . __kwargs <EOL> def __bootstrap ( self ) : <EOL> try : <EOL> self . __bootstrap_inner ( ) <EOL> except : <EOL> if self . __daemonic and _sys is None : <EOL> return <EOL> raise <EOL> def __bootstrap_inner ( self ) : <EOL> try : <EOL> self . __ident = _get_ident ( ) <EOL> self . __started . set ( ) <EOL> _active_limbo_lock . acquire ( ) <EOL> _active [ self . __ident ] = self <EOL> del _limbo [ self ] <EOL> _active_limbo_lock . release ( ) <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> if _trace_hook : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> _sys . settrace ( _trace_hook ) <EOL> if _profile_hook : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> _sys . setprofile ( _profile_hook ) <EOL> try : <EOL> self . run ( ) <EOL> except SystemExit : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> except : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> if _sys : <EOL> _sys . stderr . write ( "<STR_LIT>" % <EOL> ( self . name , _format_exc ( ) ) ) <EOL> else : <EOL> exc_type , exc_value , exc_tb = self . __exc_info ( ) <EOL> try : <EOL> print >> self . __stderr , ( <EOL> "<STR_LIT>" + self . name + <EOL> "<STR_LIT>" ) <EOL> print >> self . __stderr , ( <EOL> "<STR_LIT>" ) <EOL> while exc_tb : <EOL> print >> self . __stderr , ( <EOL> '<STR_LIT>' % <EOL> ( exc_tb . tb_frame . f_code . co_filename , <EOL> exc_tb . tb_lineno , <EOL> exc_tb . tb_frame . f_code . co_name ) ) <EOL> exc_tb = exc_tb . tb_next <EOL> print >> self . __stderr , ( "<STR_LIT>" % ( exc_type , exc_value ) ) <EOL> finally : <EOL> del exc_type , exc_value , exc_tb <EOL> else : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> finally : <EOL> self . __exc_clear ( ) <EOL> finally : <EOL> with _active_limbo_lock : <EOL> self . __stop ( ) <EOL> try : <EOL> del _active [ _get_ident ( ) ] <EOL> except : <EOL> pass <EOL> def __stop ( self ) : <EOL> self . __block . acquire ( ) <EOL> self . __stopped = True <EOL> self . __block . notify_all ( ) <EOL> self . __block . release ( ) <EOL> def __delete ( self ) : <EOL> "<STR_LIT>" <EOL> try : <EOL> with _active_limbo_lock : <EOL> del _active [ _get_ident ( ) ] <EOL> except KeyError : <EOL> if '<STR_LIT>' not in _sys . modules : <EOL> raise <EOL> def join ( self , timeout = None ) : <EOL> if not self . __initialized : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if not self . __started . is_set ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if self is current_thread ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if __debug__ : <EOL> if not self . __stopped : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> self . __block . acquire ( ) <EOL> try : <EOL> if timeout is None : <EOL> while not self . __stopped : <EOL> self . __block . wait ( ) <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> else : <EOL> deadline = _time ( ) + timeout <EOL> while not self . __stopped : <EOL> delay = deadline - _time ( ) <EOL> if delay <= <NUM_LIT:0> : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> break <EOL> self . __block . wait ( delay ) <EOL> else : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> finally : <EOL> self . __block . release ( ) <EOL> @ property <EOL> def name ( self ) : <EOL> assert self . __initialized , "<STR_LIT>" <EOL> return self . __name <EOL> @ name . setter <EOL> def name ( self , name ) : <EOL> assert self . __initialized , "<STR_LIT>" <EOL> self . __name = str ( name ) <EOL> @ property <EOL> def ident ( self ) : <EOL> assert self . __initialized , "<STR_LIT>" <EOL> return self . __ident <EOL> def isAlive ( self ) : <EOL> assert self . __initialized , "<STR_LIT>" <EOL> return self . __started . is_set ( ) and not self . __stopped <EOL> is_alive = isAlive <EOL> @ property <EOL> def daemon ( self ) : <EOL> assert self . __initialized , "<STR_LIT>" <EOL> return self . __daemonic <EOL> @ daemon . setter <EOL> def daemon ( self , daemonic ) : <EOL> if not self . __initialized : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> if self . __started . is_set ( ) : <EOL> raise RuntimeError ( "<STR_LIT>" ) ; <EOL> self . __daemonic = daemonic <EOL> def isDaemon ( self ) : <EOL> return self . daemon <EOL> def setDaemon ( self , daemonic ) : <EOL> self . daemon = daemonic <EOL> def getName ( self ) : <EOL> return self . name <EOL> def setName ( self , name ) : <EOL> self . name = name <EOL> def Timer ( * args , ** kwargs ) : <EOL> return _Timer ( * args , ** kwargs ) <EOL> class _Timer ( Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , interval , function , args = [ ] , kwargs = { } ) : <EOL> Thread . __init__ ( self ) <EOL> self . interval = interval <EOL> self . function = function <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> self . finished = Event ( ) <EOL> def cancel ( self ) : <EOL> """<STR_LIT>""" <EOL> self . finished . set ( ) <EOL> def run ( self ) : <EOL> self . finished . wait ( self . interval ) <EOL> if not self . finished . is_set ( ) : <EOL> self . function ( * self . args , ** self . kwargs ) <EOL> self . finished . set ( ) <EOL> class _MainThread ( Thread ) : <EOL> def __init__ ( self ) : <EOL> Thread . __init__ ( self , name = "<STR_LIT>" ) <EOL> self . _Thread__started . set ( ) <EOL> _active_limbo_lock . acquire ( ) <EOL> _active [ _get_ident ( ) ] = self <EOL> _active_limbo_lock . release ( ) <EOL> def _set_daemon ( self ) : <EOL> return False <EOL> def _exitfunc ( self ) : <EOL> self . _Thread__stop ( ) <EOL> t = _pickSomeNonDaemonThread ( ) <EOL> if t : <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> while t : <EOL> t . join ( ) <EOL> t = _pickSomeNonDaemonThread ( ) <EOL> if __debug__ : <EOL> self . _note ( "<STR_LIT>" , self ) <EOL> self . _Thread__delete ( ) <EOL> def _pickSomeNonDaemonThread ( ) : <EOL> for t in enumerate ( ) : <EOL> if not t . daemon and t . is_alive ( ) : <EOL> return t <EOL> return None <EOL> class _DummyThread ( Thread ) : <EOL> def __init__ ( self ) : <EOL> Thread . __init__ ( self , name = _newname ( "<STR_LIT>" ) ) <EOL> del self . _Thread__block <EOL> self . _Thread__started . set ( ) <EOL> _active_limbo_lock . acquire ( ) <EOL> _active [ _get_ident ( ) ] = self <EOL> _active_limbo_lock . release ( ) <EOL> def _set_daemon ( self ) : <EOL> return True <EOL> def join ( self , timeout = None ) : <EOL> assert False , "<STR_LIT>" <EOL> def currentThread ( ) : <EOL> try : <EOL> return _active [ _get_ident ( ) ] <EOL> except KeyError : <EOL> return _DummyThread ( ) <EOL> current_thread = currentThread <EOL> def activeCount ( ) : <EOL> _active_limbo_lock . acquire ( ) <EOL> count = len ( _active ) + len ( _limbo ) <EOL> _active_limbo_lock . release ( ) <EOL> return count <EOL> active_count = activeCount <EOL> def enumerate ( ) : <EOL> _active_limbo_lock . acquire ( ) <EOL> active = _active . values ( ) + _limbo . values ( ) <EOL> _active_limbo_lock . release ( ) <EOL> return active <EOL> from thread import stack_size <EOL> _shutdown = _MainThread ( ) . _exitfunc <EOL> try : <EOL> from thread import _local as local <EOL> except ImportError : <EOL> from _threading_local import local <EOL> def _after_fork ( ) : <EOL> global _active_limbo_lock <EOL> _active_limbo_lock = _allocate_lock ( ) <EOL> new_active = { } <EOL> current = current_thread ( ) <EOL> with _active_limbo_lock : <EOL> for thread in _active . itervalues ( ) : <EOL> if thread is current : <EOL> ident = _get_ident ( ) <EOL> thread . _Thread__ident = ident <EOL> new_active [ ident ] = thread <EOL> else : <EOL> thread . _Thread__stopped = True <EOL> _limbo . clear ( ) <EOL> _active . clear ( ) <EOL> _active . update ( new_active ) <EOL> assert len ( _active ) == <NUM_LIT:1> <EOL> def _test ( ) : <EOL> class BoundedQueue ( _Verbose ) : <EOL> def __init__ ( self , limit ) : <EOL> _Verbose . __init__ ( self ) <EOL> self . mon = RLock ( ) <EOL> self . rc = Condition ( self . mon ) <EOL> self . wc = Condition ( self . mon ) <EOL> self . limit = limit <EOL> self . queue = deque ( ) <EOL> def put ( self , item ) : <EOL> self . mon . acquire ( ) <EOL> while len ( self . queue ) >= self . limit : <EOL> self . _note ( "<STR_LIT>" , item ) <EOL> self . wc . wait ( ) <EOL> self . queue . append ( item ) <EOL> self . _note ( "<STR_LIT>" , <EOL> item , len ( self . queue ) ) <EOL> self . rc . notify ( ) <EOL> self . mon . release ( ) <EOL> def get ( self ) : <EOL> self . mon . acquire ( ) <EOL> while not self . queue : <EOL> self . _note ( "<STR_LIT>" ) <EOL> self . rc . wait ( ) <EOL> item = self . queue . popleft ( ) <EOL> self . _note ( "<STR_LIT>" , item , len ( self . queue ) ) <EOL> self . wc . notify ( ) <EOL> self . mon . release ( ) <EOL> return item <EOL> class ProducerThread ( Thread ) : <EOL> def __init__ ( self , queue , quota ) : <EOL> Thread . __init__ ( self , name = "<STR_LIT>" ) <EOL> self . queue = queue <EOL> self . quota = quota <EOL> def run ( self ) : <EOL> from random import random <EOL> counter = <NUM_LIT:0> <EOL> while counter < self . quota : <EOL> counter = counter + <NUM_LIT:1> <EOL> self . queue . put ( "<STR_LIT>" % ( self . name , counter ) ) <EOL> _sleep ( random ( ) * <NUM_LIT> ) <EOL> class ConsumerThread ( Thread ) : <EOL> def __init__ ( self , queue , count ) : <EOL> Thread . __init__ ( self , name = "<STR_LIT>" ) <EOL> self . queue = queue <EOL> self . count = count <EOL> def run ( self ) : <EOL> while self . count > <NUM_LIT:0> : <EOL> item = self . queue . get ( ) <EOL> print item <EOL> self . count = self . count - <NUM_LIT:1> <EOL> NP = <NUM_LIT:3> <EOL> QL = <NUM_LIT:4> <EOL> NI = <NUM_LIT:5> <EOL> Q = BoundedQueue ( QL ) <EOL> P = [ ] <EOL> for i in range ( NP ) : <EOL> t = ProducerThread ( Q , NI ) <EOL> t . name = ( "<STR_LIT>" % ( i + <NUM_LIT:1> ) ) <EOL> P . append ( t ) <EOL> C = ConsumerThread ( Q , NI * NP ) <EOL> for t in P : <EOL> t . start ( ) <EOL> _sleep ( <NUM_LIT> ) <EOL> C . start ( ) <EOL> for t in P : <EOL> t . join ( ) <EOL> C . join ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> _test ( ) </s>
