#NEWLINE# def run_process( child_id , test_file = None , recursive = False , num_children = 3 ) : #NEWLINE# #INDENT# def send( msg ) : #NEWLINE# #INDENT# sys . stdout . write ( '{0}:{1}\n' . format ( child_id , msg ) ) #NEWLINE# sys . stdout . flush ( ) #NEWLINE# #UNINDENT# names = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# signals = 'HUP QUIT INT TERM USR1 USR2' . split ( ) #NEWLINE# exit_signals = set ( 'INT TERM' . split ( ) ) #NEWLINE# children = [ ] #NEWLINE# if ( not isinstance ( child_id , int ) ) : #NEWLINE# #INDENT# child_id = 0 #NEWLINE# #UNINDENT# if ( ( child_id == 0 ) or ( recursive and ( child_id < 2 ) ) ) : #NEWLINE# #INDENT# for i in range ( num_children ) : #NEWLINE# #INDENT# new_child_id = ( ( ( child_id * 10 ) + i ) + 1 ) #NEWLINE# p = multiprocessing . Process ( target = run_process , args = ( new_child_id , ) , kwargs = { #NEWLINE# #INDENT# 'recursive' : recursive , #NEWLINE# 'num_children' : num_children , #NEWLINE# #UNINDENT# } ) #NEWLINE# p . daemon = ( not ( recursive and ( new_child_id < 2 ) ) ) #NEWLINE# p . start ( ) #NEWLINE# children . append ( p ) #NEWLINE# #UNINDENT# #UNINDENT# def callback( sig , frame = None ) : #NEWLINE# #INDENT# global exiting #NEWLINE# name = names [ sig ] #NEWLINE# send ( name ) #NEWLINE# if ( name in exit_signals ) : #NEWLINE# #INDENT# exiting = True #NEWLINE# #UNINDENT# #UNINDENT# for signal_name in signals : #NEWLINE# #INDENT# signum = getattr ( signal , ( 'SIG%s' % signal_name ) ) #NEWLINE# names [ signum ] = signal_name #NEWLINE# signal . signal ( signum , callback ) #NEWLINE# #UNINDENT# send ( 'STARTED' ) #NEWLINE# while ( not exiting ) : #NEWLINE# #INDENT# signal . pause ( ) #NEWLINE# #UNINDENT# send ( 'EXITING' )