部門內部的全部Web應用,restful服務等都是採用Python開發的,主要用到了bottle框架和django框架。 python
這兩個框架都是採用的自身的Web Server運行的,在高負載的狀況下響應比較慢。改成Tornado做爲Web Server來承載應用。 web
1. Bottle應用 django
from bottle import run import argparse if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("-H", "--host", help="Host to bind the API server on", default="localhost", action="store", required=False) parser.add_argument("-p", "--port", help="Port to bind the API server on", default=8090, action="store", required=False) args = parser.parse_args() run(server="tornado",host=args.host, port=args.port)
#!/usr/bin/env python from tornado.options import options, define, parse_command_line import django.core.handlers.wsgi import tornado.httpserver import tornado.ioloop import tornado.web import tornado.wsgi import os import sys define('port', type=int, default=8000) def main(): os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.settings") parse_command_line() wsgi_app = tornado.wsgi.WSGIContainer(django.core.handlers.wsgi.WSGIHandler()) tornado_app = tornado.web.Application([ (r'/static/(.*)', tornado.web.StaticFileHandler, {'path': os.path.join(os.path.join(os.getcwd()),"static")}), ('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)), ]) server = tornado.httpserver.HTTPServer(tornado_app) server.listen(options.port) tornado.ioloop.IOLoop.instance().start() if __name__ == '__main__': main()
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "web.settings")Django的配置,不可缺乏
(r'/static/(.*)', tornado.web.StaticFileHandler, {'path': os.path.join(os.path.join(os.getcwd()),"static")}),用於配置靜態文件的路由,不可缺乏,同時必須將該route放在
('.*', tornado.web.FallbackHandler, dict(fallback=wsgi_app)),前面