#NEWLINE# def serve_forever( host , port , childnum ) : #NEWLINE# #INDENT# listen_sock = socket . socket ( socket . AF_INET , socket . SOCK_STREAM ) #NEWLINE# listen_sock . setsockopt ( socket . SOL_SOCKET , socket . SO_REUSEADDR , 1 ) #NEWLINE# listen_sock . setblocking ( 0 ) #NEWLINE# listen_sock . bind ( ( host , port ) ) #NEWLINE# listen_sock . listen ( BACKLOG ) #NEWLINE# print ( ( 'Listening on port %d ...' % port ) ) #NEWLINE# ( main_rlist , wlist , elist ) = ( [ listen_sock ] , [ ] , [ ] ) #NEWLINE# for index in range ( childnum ) : #NEWLINE# #INDENT# create_child ( index , listen_sock ) #NEWLINE# main_rlist . append ( CHILDREN [ index ] [ 'pipe' ] ) #NEWLINE# #UNINDENT# FREE_CHILD_COUNT = childnum #NEWLINE# while True : #NEWLINE# #INDENT# rlist = main_rlist . copy ( ) #NEWLINE# if ( FREE_CHILD_COUNT == 0 ) : #NEWLINE# #INDENT# rlist . remove ( listen_sock ) #NEWLINE# #UNINDENT# ( readables , writables , exceptions ) = select . select ( rlist , wlist , elist ) #NEWLINE# if ( listen_sock in readables ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# ( conn , client_address ) = listen_sock . accept ( ) #NEWLINE# #UNINDENT# except IOError as e : #NEWLINE# #INDENT# ( code , msg ) = e . args #NEWLINE# if ( code == errno . EINTR ) : #NEWLINE# #INDENT# continue #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# #UNINDENT# for child in CHILDREN : #NEWLINE# #INDENT# if ( client_address [ 'status' ] == FREE ) : #NEWLINE# #INDENT# child [ 'status' ] = BUSY #NEWLINE# write_fd ( child [ 'pipe' ] , conn . fileno ( ) ) #NEWLINE# conn . close ( ) #NEWLINE# FREE_CHILD_COUNT -= 1 #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# raise Exception ( 'No free child found' ) #NEWLINE# #UNINDENT# #UNINDENT# for child in CHILDREN : #NEWLINE# #INDENT# child_pipe = child [ 'pipe' ] #NEWLINE# if ( child_pipe in readables ) : #NEWLINE# #INDENT# data = child_pipe . recv ( 1 ) #NEWLINE# if ( not data ) : #NEWLINE# #INDENT# raise Exception ( 'Child terminated unexpectedly' ) #NEWLINE# #UNINDENT# child [ 'status' ] = FREE #NEWLINE# FREE_CHILD_COUNT += 1