<s> """<STR_LIT>""" <EOL> import functools <EOL> import sys <EOL> import gevent <EOL> from gevent import Greenlet <EOL> from gevent . event import AsyncResult as g_AsyncResult <EOL> class NamedGreenlet ( Greenlet ) : <EOL> def __init__ ( self , run = None , * args , ** kwargs ) : <EOL> self . name = None <EOL> Greenlet . __init__ ( self , run , * args , ** kwargs ) <EOL> @ classmethod <EOL> def spawn ( cls , name , * args , ** kwargs ) : <EOL> g = cls ( * args , ** kwargs ) <EOL> g . name = name <EOL> g . start ( ) <EOL> return g <EOL> def __repr__ ( self ) : <EOL> if self . name : <EOL> return self . name <EOL> else : <EOL> return NamedGreenlet . __repr__ ( self ) <EOL> class AsyncResult ( g_AsyncResult ) : <EOL> @ staticmethod <EOL> def WhenAll ( ars ) : <EOL> """<STR_LIT>""" <EOL> ret = AsyncResult ( ) <EOL> num_ars = len ( ars ) <EOL> total = [ num_ars ] <EOL> results = [ None ] * num_ars <EOL> def complete ( _n , _ar ) : <EOL> if _ar . exception : <EOL> ret . set_exception ( _ar . exception ) <EOL> elif not ret . ready ( ) : <EOL> total [ <NUM_LIT:0> ] -= <NUM_LIT:1> <EOL> results [ _n ] = _ar . value <EOL> if total [ <NUM_LIT:0> ] == <NUM_LIT:0> : <EOL> ret . set ( results ) <EOL> for n , ar in enumerate ( ars ) : <EOL> ar . rawlink ( functools . partial ( complete , n ) ) <EOL> return ret <EOL> @ staticmethod <EOL> def WhenAny ( ars ) : <EOL> """<STR_LIT>""" <EOL> ready_ars = [ ar for ar in ars if ar . ready ( ) ] <EOL> if ready_ars : <EOL> return ready_ars [ <NUM_LIT:0> ] <EOL> ret = AsyncResult ( ) <EOL> total = [ len ( ars ) ] <EOL> def complete ( _ar ) : <EOL> total [ <NUM_LIT:0> ] -= <NUM_LIT:1> <EOL> if total [ <NUM_LIT:0> ] == <NUM_LIT:0> and _ar . exception : <EOL> ret . set_exception ( _ar . exception ) <EOL> elif not ret . ready ( ) and _ar . successful ( ) : <EOL> ret . set ( _ar . value ) <EOL> for ar in ars : <EOL> ar . rawlink ( complete ) <EOL> return ret <EOL> @ staticmethod <EOL> def FromValue ( val ) : <EOL> if val is None : <EOL> return AsyncResult . Complete ( ) <EOL> else : <EOL> ar = AsyncResult ( ) <EOL> ar . set ( val ) <EOL> return ar <EOL> @ staticmethod <EOL> def Complete ( ) : <EOL> """<STR_LIT>""" <EOL> return _COMPLETE <EOL> @ staticmethod <EOL> def CompleteIn ( n ) : <EOL> """<STR_LIT>""" <EOL> ar = AsyncResult ( ) <EOL> def helper ( ) : <EOL> ar . set ( ) <EOL> g = Greenlet ( helper ) <EOL> g . start_later ( float ( n ) ) <EOL> return ar <EOL> def _SafeLinkHelper ( self , fn ) : <EOL> try : <EOL> self . set ( fn ( ) ) <EOL> except : <EOL> self . set_exception ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) <EOL> def SafeLink ( self , fn ) : <EOL> """<STR_LIT>""" <EOL> gevent . spawn ( self . _SafeLinkHelper , fn ) <EOL> def ContinueWith ( self , fn , on_hub = True ) : <EOL> cw_ar = AsyncResult ( ) <EOL> def continue_with_callback ( _ar ) : <EOL> def run ( ) : <EOL> try : <EOL> val = fn ( _ar ) <EOL> cw_ar . set ( val ) <EOL> except : <EOL> cw_ar . set_exception ( sys . exc_info ( ) [ <NUM_LIT:1> ] ) <EOL> if on_hub : <EOL> run ( ) <EOL> else : <EOL> gevent . spawn ( run ) <EOL> self . rawlink ( continue_with_callback ) <EOL> return cw_ar <EOL> def Map ( self , fn ) : <EOL> def mapper ( _ ) : <EOL> if self . exception : <EOL> return self <EOL> else : <EOL> return fn ( self . value ) <EOL> return self . ContinueWith ( mapper ) . Unwrap ( ) <EOL> def _UnwrapHelper ( self , target ) : <EOL> if self . ready ( ) : <EOL> if self . exception : <EOL> target . set_exception ( self . exception ) <EOL> else : <EOL> if isinstance ( self . value , AsyncResult ) : <EOL> self . value . _UnwrapHelper ( target ) <EOL> else : <EOL> target . set ( self . value ) <EOL> else : <EOL> self . rawlink ( <EOL> functools . partial ( AsyncResult . _UnwrapHelper , target = target ) ) <EOL> def Unwrap ( self ) : <EOL> unwrapped_ar = AsyncResult ( ) <EOL> self . _UnwrapHelper ( unwrapped_ar ) <EOL> return unwrapped_ar <EOL> @ staticmethod <EOL> def TryGet ( val ) : <EOL> if isinstance ( val , AsyncResult ) : <EOL> return val . get ( ) <EOL> else : <EOL> return val <EOL> @ staticmethod <EOL> def Run ( fn ) : <EOL> ar = AsyncResult ( ) <EOL> ar . SafeLink ( fn ) <EOL> return ar <EOL> @ staticmethod <EOL> def RunInline ( fn ) : <EOL> ar = AsyncResult ( ) <EOL> ar . _SafeLinkHelper ( fn ) <EOL> return ar <EOL> _COMPLETE = AsyncResult ( ) <EOL> _COMPLETE . set ( ) <EOL> class NoopTimeout ( object ) : <EOL> def start ( self ) : pass <EOL> def cancel ( self ) : pass </s>
