<s> """<STR_LIT>""" <EOL> from coro . _coro import * <EOL> from coro . _coro import _yield <EOL> from coro import signal_handler <EOL> from coro import optional <EOL> from coro import tb <EOL> import signal <EOL> import sys <EOL> import time <EOL> import os <EOL> UNAME = os . uname ( ) [ <NUM_LIT:0> ] <EOL> import coro . dns . cache <EOL> coro . dns . cache . install ( ) <EOL> compact_traceback = tb . traceback_string <EOL> traceback_data = tb . traceback_data <EOL> def default_exception_notifier ( ) : <EOL> me = current ( ) <EOL> print_stderr ( <EOL> '<STR_LIT>' % ( <EOL> me . id , <EOL> me . name , <EOL> compact_traceback ( ) , <EOL> ) <EOL> ) <EOL> set_exception_notifier ( default_exception_notifier ) <EOL> class InParallelError ( Exception ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , result_list ) : <EOL> self . result_list = result_list <EOL> Exception . __init__ ( self , result_list ) <EOL> SUCCESS = '<STR_LIT:success>' <EOL> FAILURE = '<STR_LIT>' <EOL> def _in_parallel_wrap ( result_list , i , sem , ( fun , args ) ) : <EOL> try : <EOL> result = ( SUCCESS , fun ( * args ) ) <EOL> except : <EOL> result = ( FAILURE , sys . exc_info ( ) ) <EOL> result_list [ i ] = result <EOL> sem . release ( <NUM_LIT:1> ) <EOL> def in_parallel ( fun_arg_list ) : <EOL> """<STR_LIT>""" <EOL> n = len ( fun_arg_list ) <EOL> if n == <NUM_LIT:0> : <EOL> return [ ] <EOL> result_list = [ None ] * n <EOL> sem = inverted_semaphore ( n ) <EOL> for i in xrange ( n ) : <EOL> spawn ( <EOL> _in_parallel_wrap , <EOL> result_list , <EOL> i , <EOL> sem , <EOL> fun_arg_list [ i ] <EOL> ) <EOL> sem . block_till_zero ( ) <EOL> for i in xrange ( n ) : <EOL> if result_list [ i ] [ <NUM_LIT:0> ] is FAILURE : <EOL> raise InParallelError ( result_list ) <EOL> return [ x [ <NUM_LIT:1> ] for x in result_list ] <EOL> tick_update_interval = <NUM_LIT> <EOL> def tick_updater ( ) : <EOL> """<STR_LIT>""" <EOL> global tick_update_interval <EOL> while <NUM_LIT:1> : <EOL> sleep_relative ( tick_update_interval ) <EOL> tsc_time . update_time_relation ( ) <EOL> def waitpid ( pid ) : <EOL> """<STR_LIT>""" <EOL> if UNAME == "<STR_LIT>" : <EOL> status = <NUM_LIT:0> <EOL> while status == <NUM_LIT:0> : <EOL> returned_pid , status = os . waitpid ( pid , os . WNOHANG ) <EOL> sleep_relative ( <NUM_LIT:4> ) <EOL> return returned_pid , status <EOL> else : <EOL> me = current ( ) <EOL> wait_for ( pid , EVFILT . PROC , fflags = NOTE . EXIT ) <EOL> return os . waitpid ( pid , <NUM_LIT:0> ) <EOL> def get_thread_by_id ( thread_id ) : <EOL> """<STR_LIT>""" <EOL> return all_threads [ thread_id ] <EOL> def where ( co ) : <EOL> """<STR_LIT>""" <EOL> f = co . get_frame ( ) <EOL> return tb . stack_string ( f ) <EOL> def where_all ( ) : <EOL> """<STR_LIT>""" <EOL> output = { } <EOL> for id , c in all_threads . items ( ) : <EOL> output [ id ] = ( c . get_name ( ) , c , where ( c ) ) <EOL> return output <EOL> _original_spawn = spawn <EOL> def spawn ( fun , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> thread_name = kwargs [ '<STR_LIT>' ] <EOL> del kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> thread_name = '<STR_LIT:%s>' % ( fun , ) <EOL> return _original_spawn ( fun , * args , ** kwargs ) . set_name ( thread_name ) <EOL> _original_new = new <EOL> def new ( fun , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in kwargs : <EOL> thread_name = kwargs [ '<STR_LIT>' ] <EOL> del kwargs [ '<STR_LIT>' ] <EOL> else : <EOL> thread_name = '<STR_LIT:%s>' % ( fun , ) <EOL> return _original_new ( fun , * args , ** kwargs ) . set_name ( thread_name ) <EOL> import coro . clocks . tsc_time as tsc_time <EOL> ticks_per_sec = tsc_time . ticks_per_sec <EOL> ticks_per_usec = tsc_time . ticks_per_usec <EOL> microseconds = <NUM_LIT> <EOL> absolute_time_to_ticks = tsc_time . usec_to_ticks <EOL> ticks_to_absolute_time = tsc_time . ticks_to_usec <EOL> absolute_time_to_ticks_safe = tsc_time . usec_to_ticks_safe <EOL> ticks_to_absolute_time_safe = tsc_time . ticks_to_usec_safe <EOL> absolute_secs_to_ticks = tsc_time . sec_to_ticks <EOL> ticks_to_absolute_secs = tsc_time . ticks_to_sec <EOL> get_now = tsc_time . rdtsc <EOL> update_time_relation = tsc_time . update_time_relation <EOL> def get_usec ( ) : <EOL> """<STR_LIT>""" <EOL> return tsc_time . ticks_to_usec ( get_now ( ) ) <EOL> def ctime_ticks ( t ) : <EOL> """<STR_LIT>""" <EOL> return tsc_time . TSC_from_ticks ( t ) . ctime ( ) <EOL> def ctime_usec ( u ) : <EOL> """<STR_LIT>""" <EOL> return tsc_time . TSC_from_posix_usec ( u ) . ctime ( ) <EOL> now = get_now ( ) <EOL> now_usec = get_usec ( ) <EOL> _original_with_timeout = with_timeout <EOL> with_timeout = optional . with_timeout <EOL> _original_sleep_relative = sleep_relative <EOL> sleep_relative = optional . sleep_relative <EOL> def install_thread_emulation ( ) : <EOL> """<STR_LIT>""" <EOL> import coro . emulation . socket <EOL> import coro . emulation . thread <EOL> import coro . emulation . threading <EOL> sys . modules [ '<STR_LIT>' ] = coro . emulation . thread <EOL> sys . modules [ '<STR_LIT>' ] = coro . emulation . threading <EOL> sys . modules [ '<STR_LIT>' ] = coro . emulation . socket <EOL> _original_event_loop = event_loop <EOL> install_signal_handlers = True <EOL> event_loop_is_running = False <EOL> def coro_is_running ( ) : <EOL> """<STR_LIT>""" <EOL> return event_loop_is_running <EOL> def sigterm_handler ( * _unused_args ) : <EOL> _coro . set_exit ( ) <EOL> def event_loop ( timeout = <NUM_LIT:30> ) : <EOL> """<STR_LIT>""" <EOL> global event_loop_is_running , with_timeout , sleep_relative <EOL> time . time , time . original_time = tsc_time . now_raw_posix_fsec , time . time <EOL> with_timeout = _original_with_timeout <EOL> sleep_relative = _original_sleep_relative <EOL> if install_signal_handlers : <EOL> signal_handler . register ( signal . SIGTERM , sigterm_handler ) <EOL> signal_handler . register ( signal . SIGINT , sigterm_handler ) <EOL> spawn ( tick_updater ) . set_name ( '<STR_LIT>' ) <EOL> try : <EOL> event_loop_is_running = True <EOL> _original_event_loop ( timeout ) <EOL> finally : <EOL> event_loop_is_running = False <EOL> time . time = time . original_time </s>
