<s> """<STR_LIT>""" <EOL> import psutil <EOL> import tornado <EOL> import sys <EOL> import signal <EOL> from tornado . options import define <EOL> from tornado . options import options <EOL> from tornado . util import import_object <EOL> import assembly <EOL> from twork . options import setup_options <EOL> from twork . utils import setup_log , gen_logger <EOL> from twork . web . server import HTTPServer <EOL> from twork . timer . common_timer import CommonTimer <EOL> import twork . utils <EOL> define ( "<STR_LIT>" , default = None , <EOL> help = "<STR_LIT>" ) <EOL> define ( "<STR_LIT>" , default = False , type = bool , <EOL> help = "<STR_LIT>" ) <EOL> define ( "<STR_LIT>" , default = '<STR_LIT>' , type = str , <EOL> help = "<STR_LIT>" ) <EOL> def _quit ( ) : <EOL> if not tornado . ioloop . IOLoop . instance ( ) . _running : <EOL> return <EOL> CommonTimer ( ) . stop ( ) <EOL> HTTPServer ( ) . stop ( ) <EOL> tornado . ioloop . IOLoop . instance ( ) . stop ( ) <EOL> tornado . ioloop . IOLoop . instance ( ) . close ( all_fds = True ) <EOL> gen_logger . info ( '<STR_LIT>' ) <EOL> def _handle_signal ( sig , frame ) : <EOL> gen_logger . warning ( '<STR_LIT>' , sig ) <EOL> _quit ( ) <EOL> def _reopen_log ( sig , frame ) : <EOL> gen_logger . warning ( '<STR_LIT>' , sig ) <EOL> setup_log ( ) <EOL> def _setup_signal ( ) : <EOL> signal . signal ( signal . SIGPIPE , signal . SIG_IGN ) ; <EOL> signal . signal ( signal . SIGINT , _handle_signal ) <EOL> signal . signal ( signal . SIGQUIT , _handle_signal ) <EOL> signal . signal ( signal . SIGTERM , _handle_signal ) <EOL> signal . signal ( signal . SIGHUP , _handle_signal ) <EOL> signal . signal ( signal . SIGUSR1 , _reopen_log ) <EOL> def _setup_cpu_affinity ( ) : <EOL> p = psutil . Process ( ) <EOL> if options . cpu_bindings_modulo : <EOL> cpu_bindings = [ p . pid % len ( p . cpu_affinity ( ) ) ] <EOL> else : <EOL> cpu_bindings = options . cpu_bindings . split ( '<STR_LIT:U+002C>' ) <EOL> cpu_bindings = [ int ( core ) for core in cpu_bindings ] <EOL> if not cpu_bindings : <EOL> return <EOL> p . cpu_affinity ( cpu_bindings ) <EOL> gen_logger . warn ( '<STR_LIT>' , p . cpu_affinity ( ) ) <EOL> def _log_options ( ) : <EOL> if tornado . version_info < ( <NUM_LIT:3> , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) : <EOL> for key , option in options . iteritems ( ) : <EOL> gen_logger . info ( '<STR_LIT>' , key , option . value ( ) ) <EOL> else : <EOL> for key , option in options . items ( ) : <EOL> gen_logger . info ( '<STR_LIT>' , key , option ) <EOL> def main ( ) : <EOL> """<STR_LIT>""" <EOL> setup_options ( ) <EOL> setup_log ( ) <EOL> _setup_signal ( ) <EOL> gen_logger . info ( '<STR_LIT>' ) <EOL> app_name = '<STR_LIT>' <EOL> web_handlers = None <EOL> app_info = None <EOL> settings = { } <EOL> _module = None <EOL> if options . app_module is not None : <EOL> try : <EOL> _module = import_object ( options . app_module ) <EOL> web_handlers = _module . HANDLERS <EOL> app_name = _module . APP_INFO . lower ( ) <EOL> app_info = _module . APP_INFO . upper ( ) <EOL> settings = _module . SETTINGS <EOL> except ( ImportError , AttributeError ) as e : <EOL> gen_logger . error ( e , exc_info = True ) <EOL> setup_options ( ) <EOL> _log_options ( ) <EOL> twork . utils . common . define_process_title ( '<STR_LIT>' . <EOL> format ( app_name , options . env ) ) <EOL> try : <EOL> HTTPServer ( ) . start ( web_handlers , app_info = app_info , <EOL> twork_module = _module , <EOL> ** settings ) <EOL> _setup_cpu_affinity ( ) <EOL> tornado . ioloop . IOLoop . instance ( ) . start ( ) <EOL> except Exception as e : <EOL> gen_logger . error ( '<STR_LIT>' , e , exc_info = True ) <EOL> finally : <EOL> _quit ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
