<s> from pulsar import CommandNotFound <EOL> from pulsar . utils . pep import default_timer <EOL> from . futures import Future , chain_future <EOL> from . consts import * <EOL> __all__ = [ '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' ] <EOL> global_commands_table = { } <EOL> def get_proxy ( obj , safe = False ) : <EOL> if isinstance ( obj , ActorProxy ) : <EOL> return obj <EOL> elif hasattr ( obj , '<STR_LIT>' ) : <EOL> return get_proxy ( obj . proxy ) <EOL> else : <EOL> if safe : <EOL> return None <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % obj ) <EOL> def actor_identity ( actor ) : <EOL> return actor . aid if hasattr ( actor , '<STR_LIT>' ) else actor <EOL> def get_command ( name ) : <EOL> '''<STR_LIT>''' <EOL> command = global_commands_table . get ( name . lower ( ) ) <EOL> if not command : <EOL> raise CommandNotFound ( name ) <EOL> return command <EOL> class command : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , ack = True ) : <EOL> self . ack = ack <EOL> def __call__ ( self , f ) : <EOL> self . name = f . __name__ . lower ( ) <EOL> def command_function ( request , args , kwargs ) : <EOL> return f ( request , * args , ** kwargs ) <EOL> command_function . ack = self . ack <EOL> command_function . __name__ = self . name <EOL> command_function . __doc__ = f . __doc__ <EOL> global_commands_table [ self . name ] = command_function <EOL> return command_function <EOL> def actor_proxy_future ( aid , future = None ) : <EOL> self = Future ( ) <EOL> if isinstance ( aid , ActorProxyMonitor ) : <EOL> assert future is None <EOL> aid . callback = self <EOL> self . aid = aid . aid <EOL> else : <EOL> self . aid = aid <EOL> chain_future ( future , next = self ) <EOL> return self <EOL> class ActorProxy : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , impl ) : <EOL> self . aid = impl . aid <EOL> self . name = impl . name <EOL> self . cfg = impl . cfg <EOL> self . address = getattr ( impl , '<STR_LIT:address>' , None ) <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . name , self . aid ) <EOL> __str__ = __repr__ <EOL> @ property <EOL> def proxy ( self ) : <EOL> return self <EOL> def __eq__ ( self , o ) : <EOL> o = get_proxy ( o , True ) <EOL> return o and self . aid == o . aid <EOL> def __ne__ ( self , o ) : <EOL> return not self . __eq__ ( o ) <EOL> class ActorProxyMonitor ( ActorProxy ) : <EOL> '''<STR_LIT>''' <EOL> monitor = None <EOL> def __init__ ( self , impl ) : <EOL> self . impl = impl <EOL> self . info = { } <EOL> self . mailbox = None <EOL> self . callback = None <EOL> self . spawning_start = None <EOL> self . stopping_start = None <EOL> super ( ) . __init__ ( impl ) <EOL> @ property <EOL> def notified ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . info . get ( '<STR_LIT>' ) <EOL> @ property <EOL> def proxy ( self ) : <EOL> '''<STR_LIT>''' <EOL> return ActorProxy ( self ) <EOL> def __reduce__ ( self ) : <EOL> return self . proxy . __reduce__ ( ) <EOL> def is_alive ( self ) : <EOL> '''<STR_LIT>''' <EOL> return self . impl . is_alive ( ) <EOL> def terminate ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . impl . terminate ( ) <EOL> def join ( self , timeout = None ) : <EOL> '''<STR_LIT>''' <EOL> self . impl . join ( timeout = timeout ) <EOL> def start ( self ) : <EOL> '''<STR_LIT>''' <EOL> self . spawning_start = default_timer ( ) <EOL> self . impl . start ( ) <EOL> def should_be_alive ( self ) : <EOL> if not self . mailbox : <EOL> return default_timer ( ) - self . spawning_start > ACTOR_ACTION_TIMEOUT <EOL> else : <EOL> return True <EOL> def should_terminate ( self ) : <EOL> if self . stopping_start is None : <EOL> self . stopping_start = default_timer ( ) <EOL> return False <EOL> else : <EOL> dt = default_timer ( ) - self . stopping_start <EOL> return dt if dt >= ACTOR_ACTION_TIMEOUT else False </s>
