def _init_infrastructures(self): if self.io_loop: if not self.io_loop.initialized(): # this means self.io_loop is a customized io_loop, so `install` should be called # to make it the singleton instance #print self.io_loop.__class__.__name__ self.io_loop.install() else: self.io_loop = IOLoop.instance() application = self._make_application() http_server_options = self.get_httpserver_options() http_server = HTTPServer(application, io_loop=self.io_loop, **http_server_options) listen_kwargs = {} if settings.get('ADDRESS'): listen_kwargs['address'] = settings.get('ADDRESS') if not settings['TESTING'] and settings['DEBUG']: if settings['PROCESSES'] and settings['PROCESSES'] > 1: logging.info('Multiprocess could not be used in debug mode') try: http_server.listen(settings['PORT'], **listen_kwargs) except socket.error, e: logging.warning('socket.error detected on http_server.listen, set ADDRESS="0.0.0.0" in settings to avoid this problem') raise e
def make_http_server(self): multiprocessing = False if settings['PROCESSES'] and settings['PROCESSES'] > 1: if settings['DEBUG']: app_log.info('Multiprocess could not be used in debug mode') else: multiprocessing = True if self.io_loop: if not self.io_loop.initialized(): # this means self.io_loop is a customized io_loop, so `install` should be called # to make it the singleton instance #print self.io_loop.__class__.__name__ self.io_loop.install() else: # NOTE To support running tornado for multiple processes, we do not instance ioloop if multiprocessing is True if not multiprocessing: self.io_loop = IOLoop.instance() http_server_options = self.get_httpserver_options() http_server = HTTPServer(self.application, io_loop=self.io_loop, **http_server_options) listen_kwargs = {} if settings.get('ADDRESS'): listen_kwargs['address'] = settings.get('ADDRESS') if multiprocessing: # Multiprocessing mode try: http_server.bind(settings['PORT'], **listen_kwargs) except socket.error, e: app_log.warning('socket.error detected on http_server.listen, set ADDRESS="0.0.0.0" in settings to avoid this problem') raise e http_server.start(settings['PROCESSES'])
def _init_infrastructures(self): multiprocessing = False if settings['PROCESSES'] and settings['PROCESSES'] > 1: if settings['DEBUG']: app_log.info('Multiprocess could not be used in debug mode') else: multiprocessing = True if self.io_loop: if not self.io_loop.initialized(): # this means self.io_loop is a customized io_loop, so `install` should be called # to make it the singleton instance #print self.io_loop.__class__.__name__ self.io_loop.install() else: # NOTE To support running tornado for multiple processes, we do not instance ioloop if multiprocessing is True if not multiprocessing: self.io_loop = IOLoop.instance() application = self._make_application() http_server_options = self.get_httpserver_options() http_server = HTTPServer(application, io_loop=self.io_loop, **http_server_options) listen_kwargs = {} if settings.get('ADDRESS'): listen_kwargs['address'] = settings.get('ADDRESS') if multiprocessing: # Multiprocessing mode try: http_server.bind(settings['PORT'], **listen_kwargs) except socket.error, e: app_log.warning('socket.error detected on http_server.listen, set ADDRESS="0.0.0.0" in settings to avoid this problem') raise e http_server.start(settings['PROCESSES'])
def setup(self): """ setups before run """ testing = settings.get('TESTING') if not testing: print 'Setup torext..' # setup root logger (as early as possible) logging_kwargs = settings['LOGGING_OPTIONS'].copy() logging_kwargs['level'] = settings['LOGGING'] print 'logging kwargs: %s' % logging_kwargs set_logger('', **logging_kwargs) if testing: print 'testing, set nose formatter', logging_kwargs set_nose_formatter(logging_kwargs) # reset timezone os.environ['TZ'] = settings['TIME_ZONE'] time.tzset() # determine project name if settings._module: project = os.path.split(self.root_path)[1] if settings['PROJECT']: assert settings['PROJECT'] == project, 'PROJECT specialized in settings (%s) '\ 'should be the same as project directory name (%s)' % (settings['PROJECT'], project) else: settings['PROJECT'] = project # PROJECT should be importable as a python module if settings['PROJECT']: # add upper directory path to sys.path if not in if settings._module: _abs = os.path.abspath parent_path = os.path.dirname(self.root_path) if not _abs(parent_path) in [_abs(i) for i in sys.path]: sys.path.insert(0, parent_path) if not testing: logging.info('Add %s to sys.path' % _abs(parent_path)) try: __import__(settings['PROJECT']) if not testing: logging.debug('import package `%s` success' % settings['PROJECT']) except ImportError: raise ImportError('PROJECT could not be imported, may be app.py is outside the project' 'or there is no __init__ in the package.') self.is_setuped = True
def run(self): if not self.is_setuped: self.setup() self._init_infrastructures() if not settings.get('TESTING'): self.log_app_info() try: self.io_loop.start() except KeyboardInterrupt: print '\nStopping ioloop.. ', IOLoop.instance().stop() print 'Exit' sys.exit(0)
def run(self, application=None): if not self.is_setuped: self.setup() self._init_application(application=application) if not settings.get('TESTING'): self.log_app_info(self.application) self.make_http_server() try: self._instance_ioloop() self.io_loop.start() except KeyboardInterrupt: print('\nStopping ioloop.. ', end=' ') IOLoop.instance().stop() print('Exit') sys.exit(0)
def setup(self): """This function will be called both before `run` and testing started. """ testing = settings.get('TESTING') if testing: # Fix nose handler in testing situation. config = settings['LOGGERS'].get('', {}) set_nose_formatter(config) #print('testing, set nose formatter: {}'.format(config)) # reset timezone os.environ['TZ'] = settings['TIME_ZONE'] time.tzset() # determine project name if settings._module: project = os.path.split(self.root_path)[1] if settings['PROJECT']: assert settings['PROJECT'] == project, 'PROJECT specialized in settings (%s) '\ 'should be the same as project directory name (%s)' % (settings['PROJECT'], project) else: settings['PROJECT'] = project # PROJECT should be importable as a python module if settings['PROJECT']: # add upper directory path to sys.path if not in if settings._module: _abs = os.path.abspath parent_path = os.path.dirname(self.root_path) if not _abs(parent_path) in [_abs(i) for i in sys.path]: sys.path.insert(0, parent_path) app_log.info('Add %s to sys.path' % _abs(parent_path)) try: __import__(settings['PROJECT']) app_log.debug('import package `%s` success' % settings['PROJECT']) except ImportError: raise ImportError( 'PROJECT could not be imported, may be app.py is outside the project' 'or there is no __init__ in the package.') self.is_setuped = True
def run(self, application=None): if not self.is_setuped: self.setup() self._init_application(application=application) if not settings.get('TESTING'): self.log_app_info(self.application) self.make_http_server() try: self._instance_ioloop() self.io_loop.start() except KeyboardInterrupt: print '\nStopping ioloop.. ', IOLoop.instance().stop() print 'Exit' sys.exit(0)
def setup(self): """This function will be called both before `run` and testing started. """ testing = settings.get('TESTING') if testing: # Fix nose handler in testing situation. config = settings['LOGGERS'].get('', {}) set_nose_formatter(config) print 'testing, set nose formatter: %s' % config # reset timezone os.environ['TZ'] = settings['TIME_ZONE'] time.tzset() # determine project name if settings._module: project = os.path.split(self.root_path)[1] if settings['PROJECT']: assert settings['PROJECT'] == project, 'PROJECT specialized in settings (%s) '\ 'should be the same as project directory name (%s)' % (settings['PROJECT'], project) else: settings['PROJECT'] = project # PROJECT should be importable as a python module if settings['PROJECT']: # add upper directory path to sys.path if not in if settings._module: _abs = os.path.abspath parent_path = os.path.dirname(self.root_path) if not _abs(parent_path) in [_abs(i) for i in sys.path]: sys.path.insert(0, parent_path) app_log.info('Add %s to sys.path' % _abs(parent_path)) try: __import__(settings['PROJECT']) app_log.debug('import package `%s` success' % settings['PROJECT']) except ImportError: raise ImportError('PROJECT could not be imported, may be app.py is outside the project' 'or there is no __init__ in the package.') self.is_setuped = True