def main(): tornado.options.parse_command_line() # settings if options.settings: tornado.options.parse_config_file(options.settings) else: tornado.options.parse_config_file(os.path.join(APPSDIR, 'settings', '__init__.py')) # i18n translations # tornado.locale.load_translations(settings.TRANSLATIONDIR) # tornado.locale.get_supported_locales() # tornado.locale.set_supported_locales("en_US") # httpserver httpserver = tornado.httpserver.HTTPServer(Application(), xheaders=True) httpserver.bind(options.port, "127.0.0.1") httpserver.start(options.processor or int(1)) # WARNING: this timestamp must equal to supervisord.readear.conf stopwaitsecs = 10 # WARNING: if not or less, the server will be killed by supervisord before max_wait_seconds_before_shutdown if options.debug: MAX_WAIT_SECONDS_BEFORE_SHUTDOWN = 0 else: MAX_WAIT_SECONDS_BEFORE_SHUTDOWN = 10 # signal handler def sig_handler(sig, frame): logging.warning("Catching Signal: %s", sig) tornado.ioloop.IOLoop.instance().add_callback(shutdown) # signal handler's callback def shutdown(): logging.info("Stopping HttpServer...") httpserver.stop() # No longer accept new http traffic logging.info("IOLoop Will be Terminate in %s Seconds...", MAX_WAIT_SECONDS_BEFORE_SHUTDOWN) instance = tornado.ioloop.IOLoop.instance() deadline = time.time() + MAX_WAIT_SECONDS_BEFORE_SHUTDOWN # recursion for terminate IOLoop.instance() def terminate(): now = time.time() if now < deadline and (instance._callbacks or instance._timeouts): instance.add_timeout(now + 1, terminate) else: instance.stop() # After process all _callbacks and _timeouts, break IOLoop.instance() logging.info('Shutdown...') # process recursion terminate() # signal register signal.signal(signal.SIGINT, sig_handler) signal.signal(signal.SIGTERM, sig_handler) # start ioloop for socket, infinite before catch signal tornado.ioloop.IOLoop.instance().start() logging.info("Exit...")
def run_forever(application, port=None, num_processes=1): """run server port='8888' port='8801, 8802, 8803' """ logger = logging.getLogger('appserver') if sys.platform == 'win32': title = 'Tornado' from .win32 import set_windows_console_title, set_windows_console_size set_windows_console_title(title) set_windows_console_size(120, 24) ports = [] if port: if isinstance(port, str): ports.extends(re.split('[,;\s]+', port)) elif isinstance(port, Mapping): for p in port: ports.append(p) elif isinstance(port, int): ports.append(port) if not ports: ports.append(8888) application.setup() if num_processes <= 0: import multiprocessing num_processes = multiprocessing.cpu_count() if application.settings.get('debug', False) and num_processes != 1: num_processes = 1 logger.warn('In debug mode, it should be in single process mode') httpserver = tornado.httpserver.HTTPServer(application) for port in ports: httpserver.bind(port) httpserver.start(num_processes) ioloop = tornado.ioloop.IOLoop.instance() def sigint_int(s, f): logger.info('server is shutting') ioloop.add_callback(ioloop.stop) signal.signal(signal.SIGINT, sigint_int) print('Server Started. Press <CRTL-C> to kill server') ioloop.start() # 启动服务器 logger.info('server stopped')
def run_forever(application, port=None, num_processes=1) : """run server port='8888' port='8801, 8802, 8803' """ logger = logging.getLogger('appserver') if sys.platform == 'win32' : title = 'Tornado' from .win32 import set_windows_console_title, set_windows_console_size set_windows_console_title(title) set_windows_console_size(120, 24) ports = [] if port: if isinstance(port, str): ports.extends(re.split('[,;\s]+', port)) elif isinstance(port, Mapping): for p in port: ports.append(p) elif isinstance(port, int): ports.append(port) if not ports: ports.append(8888) application.setup() if num_processes <= 0: import multiprocessing num_processes = multiprocessing.cpu_count() if application.settings.get('debug', False) and num_processes != 1: num_processes = 1 logger.warn('In debug mode, it should be in single process mode') httpserver = tornado.httpserver.HTTPServer(application) for port in ports: httpserver.bind(port) httpserver.start(num_processes) ioloop = tornado.ioloop.IOLoop.instance() def sigint_int(s, f): logger.info('server is shutting') ioloop.add_callback(ioloop.stop) signal.signal(signal.SIGINT, sigint_int) print('Server Started. Press <CRTL-C> to kill server') ioloop.start() # 启动服务器 logger.info('server stopped')
import tornado.httpserver import tornado.ioloop import config from application import Application if __name__ == '__main__': app = Application() httpserver = tornado.httpserver.HTTPServer(app) httpserver.bind(config.options["port"]) httpserver.start(1) tornado.ioloop.IOLoop.current().start()
import tornado import tornado.web import tornado.ioloop import tornado.httpserver import tornado.options from application import Application from db import sqlite_tornado if __name__ == '__main__': app = Application() httpserver = tornado.httpserver.HTTPServer(app) httpserver.bind(8080) sqlite_tornado.db_init() httpserver.start() tornado.ioloop.IOLoop.instance().start()
import tornado.ioloop import tornado.httpserver import config from application import Application if __name__ == '__main__': app = Application() print(__file__) httpserver = tornado.httpserver.HTTPServer(app) httpserver.bind(config.options.get("port")) #httpserver.bind(8888) httpserver.start() # 默认是创建和cpu的核心数个数一致的子进程 #IOLoop IO循环的类:内部继承了epoll实现高并发 tornado.ioloop.IOLoop.current().start()
def main(): app = Myapplication() httpserver = tornado.httpserver.HTTPServer(app) httpserver.listen(config.options.get("port")) httpserver.start() tornado.ioloop.IOLoop.current().start()
def main(): tornado.options.parse_command_line() httpserver = tornado.httpserver.HTTPServer(Application()) httpserver.bind(options.port) httpserver.start(1) tornado.ioloop.IOLoop.current().start()
class IndexHandler(web.RequestHandler): def get(self, *args, **kwargs): self.write('this is for test!') # 路由 app = web.Application([(r'/', IndexHandler)]) if __name__ == "__main__": # 实例化一个http链接的服务器 httpserver = httpserver.HTTPServer(app) # 监听端口 httpserver.bind(8888) # 启动io复用\ # 启动多个子进程 httpserver.start(4) # start 参数的取值范围: tornado.ioloop.IOLoop.current().start() """ ~tornado.tcpserver.TCPServer.bind`/`~tornado.tcpserver.TCPServer.start`: simple multi-process:: server = HTTPServer(app) server.bind(8888) server.start(0) # Forks multiple sub-processes IOLoop.current().start() When using this interface, an `.IOLoop` must *not* be passed to the `HTTPServer` constructor. `~.TCPServer.start` will always start the server on the default singleton `.IOLoop`.