<s> import warnings <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> DeprecationWarning , stacklevel = <NUM_LIT:2> ) <EOL> """<STR_LIT>""" <EOL> import sys <EOL> from eventlet import api , coros , hubs <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> class LinkedExited ( Exception ) : <EOL> """<STR_LIT>""" <EOL> msg = "<STR_LIT>" <EOL> def __init__ ( self , name = None , msg = None ) : <EOL> self . name = name <EOL> if msg is None : <EOL> msg = self . msg % self . name <EOL> Exception . __init__ ( self , msg ) <EOL> class LinkedCompleted ( LinkedExited ) : <EOL> """<STR_LIT>""" <EOL> msg = "<STR_LIT>" <EOL> class LinkedFailed ( LinkedExited ) : <EOL> """<STR_LIT>""" <EOL> msg = "<STR_LIT>" <EOL> def __init__ ( self , name , typ , value = None , tb = None ) : <EOL> msg = self . msg % ( name , typ . __name__ ) <EOL> LinkedExited . __init__ ( self , name , msg ) <EOL> class LinkedKilled ( LinkedFailed ) : <EOL> """<STR_LIT>""" <EOL> msg = """<STR_LIT>""" <EOL> def getLinkedFailed ( name , typ , value = None , tb = None ) : <EOL> if issubclass ( typ , api . GreenletExit ) : <EOL> return LinkedKilled ( name , typ , value , tb ) <EOL> return LinkedFailed ( name , typ , value , tb ) <EOL> class ProcExit ( api . GreenletExit ) : <EOL> """<STR_LIT>""" <EOL> class Link ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , listener ) : <EOL> self . listener = listener <EOL> def cancel ( self ) : <EOL> self . listener = None <EOL> def __enter__ ( self ) : <EOL> pass <EOL> def __exit__ ( self , * args ) : <EOL> self . cancel ( ) <EOL> class LinkToEvent ( Link ) : <EOL> def __call__ ( self , source ) : <EOL> if self . listener is None : <EOL> return <EOL> if source . has_value ( ) : <EOL> self . listener . send ( source . value ) <EOL> else : <EOL> self . listener . send_exception ( * source . exc_info ( ) ) <EOL> class LinkToGreenlet ( Link ) : <EOL> def __call__ ( self , source ) : <EOL> if source . has_value ( ) : <EOL> self . listener . throw ( LinkedCompleted ( source . name ) ) <EOL> else : <EOL> self . listener . throw ( getLinkedFailed ( source . name , * source . exc_info ( ) ) ) <EOL> class LinkToCallable ( Link ) : <EOL> def __call__ ( self , source ) : <EOL> self . listener ( source ) <EOL> def waitall ( lst , trap_errors = False , queue = None ) : <EOL> if queue is None : <EOL> queue = coros . queue ( ) <EOL> index = - <NUM_LIT:1> <EOL> for ( index , linkable ) in enumerate ( lst ) : <EOL> linkable . link ( decorate_send ( queue , index ) ) <EOL> len = index + <NUM_LIT:1> <EOL> results = [ None ] * len <EOL> count = <NUM_LIT:0> <EOL> while count < len : <EOL> try : <EOL> index , value = queue . wait ( ) <EOL> except Exception : <EOL> if not trap_errors : <EOL> raise <EOL> else : <EOL> results [ index ] = value <EOL> count += <NUM_LIT:1> <EOL> return results <EOL> class decorate_send ( object ) : <EOL> def __init__ ( self , event , tag ) : <EOL> self . _event = event <EOL> self . _tag = tag <EOL> def __repr__ ( self ) : <EOL> params = ( type ( self ) . __name__ , self . _tag , self . _event ) <EOL> return '<STR_LIT>' % params <EOL> def __getattr__ ( self , name ) : <EOL> assert name != '<STR_LIT>' <EOL> return getattr ( self . _event , name ) <EOL> def send ( self , value ) : <EOL> self . _event . send ( ( self . _tag , value ) ) <EOL> def killall ( procs , * throw_args , ** kwargs ) : <EOL> if not throw_args : <EOL> throw_args = ( ProcExit , ) <EOL> wait = kwargs . pop ( '<STR_LIT>' , False ) <EOL> if kwargs : <EOL> raise TypeError ( '<STR_LIT>' % '<STR_LIT:U+002CU+0020>' . join ( kwargs . keys ( ) ) ) <EOL> for g in procs : <EOL> if not g . dead : <EOL> hubs . get_hub ( ) . schedule_call_global ( <NUM_LIT:0> , g . throw , * throw_args ) <EOL> if wait and api . getcurrent ( ) is not hubs . get_hub ( ) . greenlet : <EOL> api . sleep ( <NUM_LIT:0> ) <EOL> class NotUsed ( object ) : <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> __repr__ = __str__ <EOL> _NOT_USED = NotUsed ( ) <EOL> def spawn_greenlet ( function , * args ) : <EOL> """<STR_LIT>""" <EOL> g = api . Greenlet ( function ) <EOL> g . parent = hubs . get_hub ( ) . greenlet <EOL> hubs . get_hub ( ) . schedule_call_global ( <NUM_LIT:0> , g . switch , * args ) <EOL> return g <EOL> class Source ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = None ) : <EOL> self . name = name <EOL> self . _value_links = { } <EOL> self . _exception_links = { } <EOL> self . value = _NOT_USED <EOL> self . _exc = None <EOL> def _repr_helper ( self ) : <EOL> result = [ ] <EOL> result . append ( repr ( self . name ) ) <EOL> if self . value is not _NOT_USED : <EOL> if self . _exc is None : <EOL> res = repr ( self . value ) <EOL> if len ( res ) > <NUM_LIT:50> : <EOL> res = res [ : <NUM_LIT:50> ] + '<STR_LIT>' <EOL> result . append ( '<STR_LIT>' % res ) <EOL> else : <EOL> result . append ( '<STR_LIT>' % ( self . _exc , ) ) <EOL> result . append ( '<STR_LIT>' % ( len ( self . _value_links ) , len ( self . _exception_links ) ) ) <EOL> return result <EOL> def __repr__ ( self ) : <EOL> klass = type ( self ) . __name__ <EOL> return '<STR_LIT>' % ( klass , hex ( id ( self ) ) , '<STR_LIT:U+0020>' . join ( self . _repr_helper ( ) ) ) <EOL> def ready ( self ) : <EOL> return self . value is not _NOT_USED <EOL> def has_value ( self ) : <EOL> return self . value is not _NOT_USED and self . _exc is None <EOL> def has_exception ( self ) : <EOL> return self . value is not _NOT_USED and self . _exc is not None <EOL> def exc_info ( self ) : <EOL> if not self . _exc : <EOL> return ( None , None , None ) <EOL> elif len ( self . _exc ) == <NUM_LIT:3> : <EOL> return self . _exc <EOL> elif len ( self . _exc ) == <NUM_LIT:1> : <EOL> if isinstance ( self . _exc [ <NUM_LIT:0> ] , type ) : <EOL> return self . _exc [ <NUM_LIT:0> ] , None , None <EOL> else : <EOL> return self . _exc [ <NUM_LIT:0> ] . __class__ , self . _exc [ <NUM_LIT:0> ] , None <EOL> elif len ( self . _exc ) == <NUM_LIT:2> : <EOL> return self . _exc [ <NUM_LIT:0> ] , self . _exc [ <NUM_LIT:1> ] , None <EOL> else : <EOL> return self . _exc <EOL> def link_value ( self , listener = None , link = None ) : <EOL> if self . ready ( ) and self . _exc is not None : <EOL> return <EOL> if listener is None : <EOL> listener = api . getcurrent ( ) <EOL> if link is None : <EOL> link = self . getLink ( listener ) <EOL> if self . ready ( ) and listener is api . getcurrent ( ) : <EOL> link ( self ) <EOL> else : <EOL> self . _value_links [ listener ] = link <EOL> if self . value is not _NOT_USED : <EOL> self . _start_send ( ) <EOL> return link <EOL> def link_exception ( self , listener = None , link = None ) : <EOL> if self . value is not _NOT_USED and self . _exc is None : <EOL> return <EOL> if listener is None : <EOL> listener = api . getcurrent ( ) <EOL> if link is None : <EOL> link = self . getLink ( listener ) <EOL> if self . ready ( ) and listener is api . getcurrent ( ) : <EOL> link ( self ) <EOL> else : <EOL> self . _exception_links [ listener ] = link <EOL> if self . value is not _NOT_USED : <EOL> self . _start_send_exception ( ) <EOL> return link <EOL> def link ( self , listener = None , link = None ) : <EOL> if listener is None : <EOL> listener = api . getcurrent ( ) <EOL> if link is None : <EOL> link = self . getLink ( listener ) <EOL> if self . ready ( ) and listener is api . getcurrent ( ) : <EOL> if self . _exc is None : <EOL> link ( self ) <EOL> else : <EOL> link ( self ) <EOL> else : <EOL> self . _value_links [ listener ] = link <EOL> self . _exception_links [ listener ] = link <EOL> if self . value is not _NOT_USED : <EOL> if self . _exc is None : <EOL> self . _start_send ( ) <EOL> else : <EOL> self . _start_send_exception ( ) <EOL> return link <EOL> def unlink ( self , listener = None ) : <EOL> if listener is None : <EOL> listener = api . getcurrent ( ) <EOL> self . _value_links . pop ( listener , None ) <EOL> self . _exception_links . pop ( listener , None ) <EOL> @ staticmethod <EOL> def getLink ( listener ) : <EOL> if hasattr ( listener , '<STR_LIT>' ) : <EOL> return LinkToGreenlet ( listener ) <EOL> if hasattr ( listener , '<STR_LIT>' ) : <EOL> return LinkToEvent ( listener ) <EOL> elif hasattr ( listener , '<STR_LIT>' ) : <EOL> return LinkToCallable ( listener ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" % ( listener , ) ) <EOL> def send ( self , value ) : <EOL> assert not self . ready ( ) , "<STR_LIT>" % self <EOL> self . value = value <EOL> self . _exc = None <EOL> self . _start_send ( ) <EOL> def _start_send ( self ) : <EOL> hubs . get_hub ( ) . schedule_call_global ( <NUM_LIT:0> , self . _do_send , self . _value_links . items ( ) , self . _value_links ) <EOL> def send_exception ( self , * throw_args ) : <EOL> assert not self . ready ( ) , "<STR_LIT>" % self <EOL> self . value = None <EOL> self . _exc = throw_args <EOL> self . _start_send_exception ( ) <EOL> def _start_send_exception ( self ) : <EOL> hubs . get_hub ( ) . schedule_call_global ( <NUM_LIT:0> , self . _do_send , self . _exception_links . items ( ) , self . _exception_links ) <EOL> def _do_send ( self , links , consult ) : <EOL> while links : <EOL> listener , link = links . pop ( ) <EOL> try : <EOL> if listener in consult : <EOL> try : <EOL> link ( self ) <EOL> finally : <EOL> consult . pop ( listener , None ) <EOL> except : <EOL> hubs . get_hub ( ) . schedule_call_global ( <NUM_LIT:0> , self . _do_send , links , consult ) <EOL> raise <EOL> def wait ( self , timeout = None , * throw_args ) : <EOL> """<STR_LIT>""" <EOL> if self . value is not _NOT_USED : <EOL> if self . _exc is None : <EOL> return self . value <EOL> else : <EOL> api . getcurrent ( ) . throw ( * self . _exc ) <EOL> if timeout is not None : <EOL> timer = api . timeout ( timeout , * throw_args ) <EOL> timer . __enter__ ( ) <EOL> if timeout == <NUM_LIT:0> : <EOL> if timer . __exit__ ( None , None , None ) : <EOL> return <EOL> else : <EOL> try : <EOL> api . getcurrent ( ) . throw ( * timer . throw_args ) <EOL> except : <EOL> if not timer . __exit__ ( * sys . exc_info ( ) ) : <EOL> raise <EOL> return <EOL> EXC = True <EOL> try : <EOL> try : <EOL> waiter = Waiter ( ) <EOL> self . link ( waiter ) <EOL> try : <EOL> return waiter . wait ( ) <EOL> finally : <EOL> self . unlink ( waiter ) <EOL> except : <EOL> EXC = False <EOL> if timeout is None or not timer . __exit__ ( * sys . exc_info ( ) ) : <EOL> raise <EOL> finally : <EOL> if timeout is not None and EXC : <EOL> timer . __exit__ ( None , None , None ) <EOL> class Waiter ( object ) : <EOL> def __init__ ( self ) : <EOL> self . greenlet = None <EOL> def send ( self , value ) : <EOL> """<STR_LIT>""" <EOL> assert api . getcurrent ( ) is hubs . get_hub ( ) . greenlet <EOL> if self . greenlet is not None : <EOL> self . greenlet . switch ( value ) <EOL> def send_exception ( self , * throw_args ) : <EOL> """<STR_LIT>""" <EOL> assert api . getcurrent ( ) is hubs . get_hub ( ) . greenlet <EOL> if self . greenlet is not None : <EOL> self . greenlet . throw ( * throw_args ) <EOL> def wait ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . greenlet is None <EOL> current = api . getcurrent ( ) <EOL> assert current is not hubs . get_hub ( ) . greenlet <EOL> self . greenlet = current <EOL> try : <EOL> return hubs . get_hub ( ) . switch ( ) <EOL> finally : <EOL> self . greenlet = None <EOL> class Proc ( Source ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name = None ) : <EOL> self . greenlet = None <EOL> Source . __init__ ( self , name ) <EOL> def _repr_helper ( self ) : <EOL> if self . greenlet is not None and self . greenlet . dead : <EOL> dead = '<STR_LIT>' <EOL> else : <EOL> dead = '<STR_LIT>' <EOL> return [ '<STR_LIT>' % ( self . greenlet , dead ) ] + Source . _repr_helper ( self ) <EOL> def __repr__ ( self ) : <EOL> klass = type ( self ) . __name__ <EOL> return '<STR_LIT>' % ( klass , '<STR_LIT:U+0020>' . join ( self . _repr_helper ( ) ) ) <EOL> def __nonzero__ ( self ) : <EOL> if self . ready ( ) : <EOL> return False <EOL> if self . greenlet is not None : <EOL> return bool ( self . greenlet ) <EOL> @ property <EOL> def dead ( self ) : <EOL> return self . ready ( ) or self . greenlet . dead <EOL> @ classmethod <EOL> def spawn ( cls , function , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> proc = cls ( ) <EOL> proc . run ( function , * args , ** kwargs ) <EOL> return proc <EOL> def run ( self , function , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> assert self . greenlet is None , "<STR_LIT>" <EOL> if self . name is None : <EOL> self . name = str ( function ) <EOL> self . greenlet = spawn_greenlet ( self . _run , function , args , kwargs ) <EOL> def _run ( self , function , args , kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> result = function ( * args , ** kwargs ) <EOL> except : <EOL> self . send_exception ( * sys . exc_info ( ) ) <EOL> raise <EOL> else : <EOL> self . send ( result ) <EOL> def throw ( self , * throw_args ) : <EOL> """<STR_LIT>""" <EOL> if not self . dead : <EOL> if not throw_args : <EOL> throw_args = ( ProcExit , ) <EOL> self . greenlet . throw ( * throw_args ) <EOL> def kill ( self , * throw_args ) : <EOL> """<STR_LIT>""" <EOL> if not self . dead : <EOL> if not throw_args : <EOL> throw_args = ( ProcExit , ) <EOL> hubs . get_hub ( ) . schedule_call_global ( <NUM_LIT:0> , self . greenlet . throw , * throw_args ) <EOL> if api . getcurrent ( ) is not hubs . get_hub ( ) . greenlet : <EOL> api . sleep ( <NUM_LIT:0> ) <EOL> spawn = Proc . spawn <EOL> def spawn_link ( function , * args , ** kwargs ) : <EOL> p = spawn ( function , * args , ** kwargs ) <EOL> p . link ( ) <EOL> return p <EOL> def spawn_link_value ( function , * args , ** kwargs ) : <EOL> p = spawn ( function , * args , ** kwargs ) <EOL> p . link_value ( ) <EOL> return p <EOL> def spawn_link_exception ( function , * args , ** kwargs ) : <EOL> p = spawn ( function , * args , ** kwargs ) <EOL> p . link_exception ( ) <EOL> return p <EOL> class wrap_errors ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , errors , func ) : <EOL> """<STR_LIT>""" <EOL> self . errors = errors <EOL> self . func = func <EOL> def __call__ ( self , * args , ** kwargs ) : <EOL> try : <EOL> return self . func ( * args , ** kwargs ) <EOL> except self . errors , ex : <EOL> return ex <EOL> def __str__ ( self ) : <EOL> return str ( self . func ) <EOL> def __repr__ ( self ) : <EOL> return repr ( self . func ) <EOL> def __getattr__ ( self , item ) : <EOL> return getattr ( self . func , item ) <EOL> class RunningProcSet ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args ) : <EOL> self . procs = set ( * args ) <EOL> if args : <EOL> for p in self . args [ <NUM_LIT:0> ] : <EOL> p . link ( lambda p : self . procs . discard ( p ) ) <EOL> def __len__ ( self ) : <EOL> return len ( self . procs ) <EOL> def __contains__ ( self , item ) : <EOL> if isinstance ( item , api . Greenlet ) : <EOL> for x in self . procs : <EOL> if x . greenlet == item : <EOL> return True <EOL> else : <EOL> return item in self . procs <EOL> def __iter__ ( self ) : <EOL> return iter ( self . procs ) <EOL> def add ( self , p ) : <EOL> self . procs . add ( p ) <EOL> p . link ( lambda p : self . procs . discard ( p ) ) <EOL> def spawn ( self , func , * args , ** kwargs ) : <EOL> p = spawn ( func , * args , ** kwargs ) <EOL> self . add ( p ) <EOL> return p <EOL> def waitall ( self , trap_errors = False ) : <EOL> while self . procs : <EOL> waitall ( self . procs , trap_errors = trap_errors ) <EOL> def killall ( self , * throw_args , ** kwargs ) : <EOL> return killall ( self . procs , * throw_args , ** kwargs ) <EOL> class Pool ( object ) : <EOL> linkable_class = Proc <EOL> def __init__ ( self , limit ) : <EOL> self . semaphore = coros . Semaphore ( limit ) <EOL> def allocate ( self ) : <EOL> self . semaphore . acquire ( ) <EOL> g = self . linkable_class ( ) <EOL> g . link ( lambda * _args : self . semaphore . release ( ) ) <EOL> return g </s>
