部门内部的所有Web应用,restful服务等都是采用Python开发的,主要用到了bottle框架和django框架。
这两个框架都是采用的自身的Web Server运行的,在高负载的情况下响应比较慢。改为Tornado作为Web Server来承载应用。
1. Bottle应用
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)),
前面