<s> import atexit <EOL> import imp <EOL> import os <EOL> import sys <EOL> import traceback <EOL> import eventlet <EOL> from eventlet import event , greenio , greenthread , patcher , timeout <EOL> from eventlet . support import six <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> EXC_CLASSES = ( Exception , timeout . Timeout ) <EOL> SYS_EXCS = ( GeneratorExit , KeyboardInterrupt , SystemExit ) <EOL> QUIET = True <EOL> socket = patcher . original ( '<STR_LIT>' ) <EOL> threading = patcher . original ( '<STR_LIT>' ) <EOL> if six . PY2 : <EOL> Queue_module = patcher . original ( '<STR_LIT>' ) <EOL> if six . PY3 : <EOL> Queue_module = patcher . original ( '<STR_LIT>' ) <EOL> Empty = Queue_module . Empty <EOL> Queue = Queue_module . Queue <EOL> _bytetosend = b'<STR_LIT:U+0020>' <EOL> _coro = None <EOL> _nthreads = int ( os . environ . get ( '<STR_LIT>' , <NUM_LIT:20> ) ) <EOL> _reqq = _rspq = None <EOL> _rsock = _wsock = None <EOL> _setup_already = False <EOL> _threads = [ ] <EOL> def tpool_trampoline ( ) : <EOL> global _rspq <EOL> while True : <EOL> try : <EOL> _c = _rsock . recv ( <NUM_LIT:1> ) <EOL> assert _c <EOL> except ValueError : <EOL> break <EOL> while not _rspq . empty ( ) : <EOL> try : <EOL> ( e , rv ) = _rspq . get ( block = False ) <EOL> e . send ( rv ) <EOL> e = rv = None <EOL> except Empty : <EOL> pass <EOL> def tworker ( ) : <EOL> global _rspq <EOL> while True : <EOL> try : <EOL> msg = _reqq . get ( ) <EOL> except AttributeError : <EOL> return <EOL> if msg is None : <EOL> return <EOL> ( e , meth , args , kwargs ) = msg <EOL> rv = None <EOL> try : <EOL> rv = meth ( * args , ** kwargs ) <EOL> except SYS_EXCS : <EOL> raise <EOL> except EXC_CLASSES : <EOL> rv = sys . exc_info ( ) <EOL> _rspq . put ( ( e , rv ) ) <EOL> msg = meth = args = kwargs = e = rv = None <EOL> _wsock . sendall ( _bytetosend ) <EOL> def execute ( meth , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> setup ( ) <EOL> my_thread = threading . currentThread ( ) <EOL> if my_thread in _threads or imp . lock_held ( ) or _nthreads == <NUM_LIT:0> : <EOL> return meth ( * args , ** kwargs ) <EOL> e = event . Event ( ) <EOL> _reqq . put ( ( e , meth , args , kwargs ) ) <EOL> rv = e . wait ( ) <EOL> if isinstance ( rv , tuple ) and len ( rv ) == <NUM_LIT:3> and isinstance ( rv [ <NUM_LIT:1> ] , EXC_CLASSES ) : <EOL> ( c , e , tb ) = rv <EOL> if not QUIET : <EOL> traceback . print_exception ( c , e , tb ) <EOL> traceback . print_stack ( ) <EOL> six . reraise ( c , e , tb ) <EOL> return rv <EOL> def proxy_call ( autowrap , f , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if kwargs . pop ( '<STR_LIT>' , False ) : <EOL> rv = f ( * args , ** kwargs ) <EOL> else : <EOL> rv = execute ( f , * args , ** kwargs ) <EOL> if isinstance ( rv , autowrap ) : <EOL> return Proxy ( rv , autowrap ) <EOL> else : <EOL> return rv <EOL> class Proxy ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , obj , autowrap = ( ) , autowrap_names = ( ) ) : <EOL> self . _obj = obj <EOL> self . _autowrap = autowrap <EOL> self . _autowrap_names = autowrap_names <EOL> def __getattr__ ( self , attr_name ) : <EOL> f = getattr ( self . _obj , attr_name ) <EOL> if not hasattr ( f , '<STR_LIT>' ) : <EOL> if isinstance ( f , self . _autowrap ) or attr_name in self . _autowrap_names : <EOL> return Proxy ( f , self . _autowrap ) <EOL> return f <EOL> def doit ( * args , ** kwargs ) : <EOL> result = proxy_call ( self . _autowrap , f , * args , ** kwargs ) <EOL> if attr_name in self . _autowrap_names and not isinstance ( result , Proxy ) : <EOL> return Proxy ( result ) <EOL> return result <EOL> return doit <EOL> def __getitem__ ( self , key ) : <EOL> return proxy_call ( self . _autowrap , self . _obj . __getitem__ , key ) <EOL> def __setitem__ ( self , key , value ) : <EOL> return proxy_call ( self . _autowrap , self . _obj . __setitem__ , key , value ) <EOL> def __deepcopy__ ( self , memo = None ) : <EOL> return proxy_call ( self . _autowrap , self . _obj . __deepcopy__ , memo ) <EOL> def __copy__ ( self , memo = None ) : <EOL> return proxy_call ( self . _autowrap , self . _obj . __copy__ , memo ) <EOL> def __call__ ( self , * a , ** kw ) : <EOL> if '<STR_LIT>' in self . _autowrap_names : <EOL> return Proxy ( proxy_call ( self . _autowrap , self . _obj , * a , ** kw ) ) <EOL> else : <EOL> return proxy_call ( self . _autowrap , self . _obj , * a , ** kw ) <EOL> def __enter__ ( self ) : <EOL> return proxy_call ( self . _autowrap , self . _obj . __enter__ ) <EOL> def __exit__ ( self , * exc ) : <EOL> return proxy_call ( self . _autowrap , self . _obj . __exit__ , * exc ) <EOL> def __eq__ ( self , rhs ) : <EOL> return self . _obj == rhs <EOL> def __hash__ ( self ) : <EOL> return self . _obj . __hash__ ( ) <EOL> def __repr__ ( self ) : <EOL> return self . _obj . __repr__ ( ) <EOL> def __str__ ( self ) : <EOL> return self . _obj . __str__ ( ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _obj ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . _obj ) <EOL> __bool__ = __nonzero__ <EOL> def __iter__ ( self ) : <EOL> it = iter ( self . _obj ) <EOL> if it == self . _obj : <EOL> return self <EOL> else : <EOL> return Proxy ( it ) <EOL> def next ( self ) : <EOL> return proxy_call ( self . _autowrap , next , self . _obj ) <EOL> __next__ = next <EOL> def setup ( ) : <EOL> global _rsock , _wsock , _coro , _setup_already , _rspq , _reqq <EOL> if _setup_already : <EOL> return <EOL> else : <EOL> _setup_already = True <EOL> assert _nthreads >= <NUM_LIT:0> , "<STR_LIT>" <EOL> if _nthreads == <NUM_LIT:0> : <EOL> import warnings <EOL> warnings . warn ( "<STR_LIT>" , RuntimeWarning ) <EOL> _reqq = Queue ( maxsize = - <NUM_LIT:1> ) <EOL> _rspq = Queue ( maxsize = - <NUM_LIT:1> ) <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> sock . bind ( ( '<STR_LIT:127.0.0.1>' , <NUM_LIT:0> ) ) <EOL> sock . listen ( <NUM_LIT:1> ) <EOL> csock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) <EOL> csock . connect ( sock . getsockname ( ) ) <EOL> csock . setsockopt ( socket . IPPROTO_TCP , socket . TCP_NODELAY , True ) <EOL> _wsock , _addr = sock . accept ( ) <EOL> _wsock . setsockopt ( socket . IPPROTO_TCP , socket . TCP_NODELAY , True ) <EOL> sock . close ( ) <EOL> _rsock = greenio . GreenSocket ( csock ) <EOL> for i in six . moves . range ( _nthreads ) : <EOL> t = threading . Thread ( target = tworker , <EOL> name = "<STR_LIT>" % i ) <EOL> t . setDaemon ( True ) <EOL> t . start ( ) <EOL> _threads . append ( t ) <EOL> _coro = greenthread . spawn_n ( tpool_trampoline ) <EOL> eventlet . sleep ( <NUM_LIT:0> ) <EOL> @ atexit . register <EOL> def killall ( ) : <EOL> global _setup_already , _rspq , _rsock , _wsock <EOL> if not _setup_already : <EOL> return <EOL> eventlet . sleep ( <NUM_LIT:0> ) <EOL> for thr in _threads : <EOL> _reqq . put ( None ) <EOL> for thr in _threads : <EOL> thr . join ( ) <EOL> del _threads [ : ] <EOL> while ( _rspq is not None ) and not _rspq . empty ( ) : <EOL> try : <EOL> ( e , rv ) = _rspq . get ( block = False ) <EOL> e . send ( rv ) <EOL> e = rv = None <EOL> except Empty : <EOL> pass <EOL> if _coro is not None : <EOL> greenthread . kill ( _coro ) <EOL> if _rsock is not None : <EOL> _rsock . close ( ) <EOL> _rsock = None <EOL> if _wsock is not None : <EOL> _wsock . close ( ) <EOL> _wsock = None <EOL> _rspq = None <EOL> _setup_already = False <EOL> def set_num_threads ( nthreads ) : <EOL> global _nthreads <EOL> _nthreads = nthreads </s>
