Home Backend Development Python Tutorial Python Web框架Tornado运行和部署

Python Web框架Tornado运行和部署

Jun 10, 2016 pm 03:05 PM
python tornado web framework

本文实例为大家分享了Python Web框架Tornado运行和部署的详细内容,供大家参考,具体内容如下

一、运行和部署
因为Tornado内置了自己的HTTPServer,运行和部署它与其他Python web框架不太一样。你需要写一个main()函数来启动服务,而不是配置一个WSGI容器来运行你的应用:

def main():
  app = make_app()
  app.listen(8888)
  IOLoop.current().start()

if __name__ == '__main__':
  main()

Copy after login

配置你的操作系统或者进程管理器来运行这个程序以启动服务。注意,增加每个进程允许打开的最大文件句柄数是可能是必要的(为了避免“Too many open files” 的错误)。为了增加这个上限(例如设置为50000 ) 你可以使用ulimit命令,修改/etc/security/limits.conf 或者设置minfds 在你的supervisord配置中。

二、进程和端口
由于Python的GIL(全局解释器锁),为了充分利用多CPU的机器,运行多个Python 进程是很有必要的。通常,最好是每个CPU运行一个进程。

Tornado包含了一个内置的多进程模式来一次启动多个进程,这需要一个在main 函数上做点微小的改变:

def main():
  app = make_app()
  server = tornado.httpserver.HTTPServer(app)
  server.bind(8888)
  server.start(0) # forks one process per cpu
  IOLoop.current().start()
Copy after login

这是最简单的方式来启动多进程并让他们共享同样的端口,虽然它有一些局限性。首先,每个子进程将有它自己的IOLoop,所以fork之前,不接触全局 IOLoop 实例是重要的(甚至是间接的)。其次,在这个模型中,很难做到零停机 (zero-downtime)更新。最后,因为所有的进程共享相同的端口,想单独监控它们就更加困难了。

对更复杂的部署,建议启动独立的进程,并让它们各自监听不同的端口, supervisord 的“进程组(process groups)”功能是一个很好的方式。当每个进程使用不同的端口,一个外部的负载均衡器,例如HAProxy或nginx通常需要对外向访客提供一个单一的地址。

三、运行在负载均衡器后面
当运行在一个负载均衡器例如nginx,建议传递xheaders=True 给 HTTPServer 的构造器。这将告诉Tornado使用类似 X-Real-IP 这样的HTTP头来获取用户的IP地址而不是把所有流量都认为来自于负载均衡器的IP地址。

这是一份原始的nginx配置文件,在结构上类似于我们在FriendFeed所使用的配置。这是假设nginx和Tornado server运行在同一台机器上的,并且四个 Tornado server 正运行在8000 - 8003端口:

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;

events {
  worker_connections 1024;
  use epoll;
}

http {
  # Enumerate all the Tornado servers here
  upstream frontends {
    server 127.0.0.1:8000;
    server 127.0.0.1:8001;
    server 127.0.0.1:8002;
    server 127.0.0.1:8003;
  }

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  access_log /var/log/nginx/access.log;

  keepalive_timeout 65;
  proxy_read_timeout 200;
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  gzip on;
  gzip_min_length 1000;
  gzip_proxied any;
  gzip_types text/plain text/html text/css text/xml
        application/x-javascript application/xml
        application/atom+xml text/javascript;

  # Only retry if there was a communication error, not a timeout
  # on the Tornado server (to avoid propagating "queries of death"
  # to all frontends)
  proxy_next_upstream error;

  server {
    listen 80;

    # Allow file uploads
    client_max_body_size 50M;

    location ^~ /static/ {
      root /var/www;
      if ($query_string) {
        expires max;
      }
    }
    location = /favicon.ico {
      rewrite (.*) /static/favicon.ico;
    }
    location = /robots.txt {
      rewrite (.*) /static/robots.txt;
    }

    location / {
      proxy_pass_header Server;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Scheme $scheme;
      proxy_pass http://frontends;
    }
  }
}

Copy after login

四、静态文件和文件缓存
Tornado中,你可以通过在应用程序中指定特殊的 static_path 来提供静态文件服务:

settings = {
  "static_path": os.path.join(os.path.dirname(__file__), "static"),
  "cookie_secret": "__TODO:_GENERATE_YOUR_OWN_RANDOM_VALUE_HERE__",
  "login_url": "/login",
  "xsrf_cookies": True,
}
application = tornado.web.Application([
  (r"/", MainHandler),
  (r"/login", LoginHandler),
  (r"/(apple-touch-icon\.png)", tornado.web.StaticFileHandler,
   dict(path=settings['static_path'])),
], **settings)
Copy after login

这些设置将自动的把所有以 /static/ 开头的请求交由static目录,例如http://localhost:8888/static/foo.png 将会通过指定的static目录提供 foo.png 文件。我们也会自动从static目录提供 /robots.txt 和 /favicon.ico (尽管它们并没有以 /static/ 前缀开始)。

在上面的设置中,我们明确的配置Tornado从 StaticFileHandler 根下获取 apple-touch-icon.png 文件,虽然文件在static文件目录中。(正则表达式捕获组必须告诉 StaticFileHandler 请求的文件名,调用捕获组把文件名作为方法的参数传递给处理程序) 你可以做同样的事情,比如从网站的根提供 sitemap.xml 文件。 当然,你也可以通过在你的HTML中使用 标签来避免伪造根目录的 apple-touch-icon.png 。

为了改善性能,通常情况下,让浏览器主动缓存静态资源是个好主意, 这样浏览器就不会发送不必要的可能在渲染页面时阻塞的 If-Modified-Since 或 Etag 请求了, Tornado使用 静态内容版本(static content versioning) 来支持此项功能。

为了使用这些功能,在你的模板中使用 static_url 方法,而不是直接在你的HTML中输入静态文件的URL:

<html>
  <head>
    <title>FriendFeed - {{ _("Home") }}</title>
  </head>
  <body>
    <div><img  src="{{ static_url("images/logo.png") }}"/ alt="Python Web框架Tornado运行和部署" ></div>
  </body>
</html>
Copy after login

static_url() 函数将把相对路径翻译成一个URI类似于 /static/images/logo.png?v=aae54.其中的 v 参数是 logo.png 内容的哈希(hash),并且它的存在使得Tornado服务向用户的浏览器发送缓存头,这将使浏览器无限期的缓存内容。

因为参数 v 是基于文件内容的,如果你更新一个文件并重启服务,它将发送一个新的 v 值,所以用户的浏览器将会自动的拉去新的文件。如果文件的内容没有改变,浏览器将会继续使用本地缓存的副本,而不会从服务器检查更新,显著的提高了渲染性能。

在生产中,你可能想提供静态文件通过一个更优的静态服务器, 比如nginx,你可以配置任何web服务器识别通过 static_url() 提供的版本标签并相应的设置缓存头。下面是我们在 FriendFeed 使用的nginx相关配置的一部分:

location /static/ {
  root /var/friendfeed/static;
  if ($query_string) {
    expires max;
  }
 }
Copy after login

五、Debug模式和自动重载
如果传递 debug=True 配置给 Application 的构造函数,应用程序将会运行在debug/开发模式。 在这个模式下,为了方便于开发的一些功能将被启用( 每一个也可以作为独立的标签使用,如果它们都被专门指定,那它们都将获得独立的优先级):

1、autoreload=True: 应用程序将会观察它的源文件是否改变,并且当任何文件改变的时候便重载它自己。这减少了在开发中需要手动重启服务的需求。然而,在debug模式下,某些错误(例如import的时候有语法错误)会导致服务 关闭,并且无法自动恢复。
2、compiled_template_cache=False: 模板将不会被缓存。
3、static_hash_cache=False: 静态文件哈希 (被 static_url 函数使用) 将不会被缓存。
4、serve_traceback=True: 当一个异常在 RequestHandler 中没有捕获,将会生成一个包含调用栈信息的错误页。
自动重载(autoreload)模式和 HTTPServer 的多进程模式不兼容,你不能给 HTTPServer.start 传递 1 以外的参数(或者调用 tornado.process.fork_processes) 当你使用自动重载模式的时候。

debug模式的自动重载功能可作为一个独立的模块位于 tornado.autoreload。以下两者可以结合使用,在语法错误之时提供额外的健壮性: 设置 autoreload=True 可以在app运行时检测文件修改,还有启动 python -m tornado.autoreload myserver.py 来捕获任意语法错误或者其他的启动时错误。

重载会丢失任何Python解释器命令行参数(-u). 因为它使用 sys.executable 和 sys.argv 重新执行Python。此外,修改这些变量将造成重载错误。

在一些平台(包括Windows 和Mac OSX 10.6之前),进程不能被“原地”更新,所以当检测到代码更新,旧服务就会退出然后启动一个新服务。这已经被公知来混淆一些IDE。

六、WSGI和Google App Engine
Tornado通常是独立运行的,不需要一个WSGI容器。然而,在一些环境中 (例如Google App Engine),只运行WSGI,应用程序不能独立运行自己的服务。在这种情况下,Tornado支持一个有限制的操作模式,不支持异步操作但允许一个Tornado's功能的子集在仅WSGI环境中。以下功能在WSGI模式下是不支持的,包括协程,@asynchronous 装饰器,AsyncHTTPClient,auth 模块和WebSockets。

你可以使用 tornado.wsgi.WSGIAdapter 把一个Tornado Application 转换成WSGI应用。在这个例子中, 配置你的WSGI容器发 现 application 对象:

import tornado.web
import tornado.wsgi

class MainHandler(tornado.web.RequestHandler):
  def get(self):
    self.write("Hello, world")

tornado_app = tornado.web.Application([
  (r"/", MainHandler),
])
application = tornado.wsgi.WSGIAdapter(tornado_app)
Copy after login

以上就是本文的全部内容,希望对大家的学习有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP and Python: Different Paradigms Explained PHP and Python: Different Paradigms Explained Apr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

Choosing Between PHP and Python: A Guide Choosing Between PHP and Python: A Guide Apr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Python: A Deep Dive into Their History PHP and Python: A Deep Dive into Their History Apr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Python vs. JavaScript: The Learning Curve and Ease of Use Python vs. JavaScript: The Learning Curve and Ease of Use Apr 16, 2025 am 12:12 AM

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

How to run sublime code python How to run sublime code python Apr 16, 2025 am 08:48 AM

To run Python code in Sublime Text, you need to install the Python plug-in first, then create a .py file and write the code, and finally press Ctrl B to run the code, and the output will be displayed in the console.

Can vs code run in Windows 8 Can vs code run in Windows 8 Apr 15, 2025 pm 07:24 PM

VS Code can run on Windows 8, but the experience may not be great. First make sure the system has been updated to the latest patch, then download the VS Code installation package that matches the system architecture and install it as prompted. After installation, be aware that some extensions may be incompatible with Windows 8 and need to look for alternative extensions or use newer Windows systems in a virtual machine. Install the necessary extensions to check whether they work properly. Although VS Code is feasible on Windows 8, it is recommended to upgrade to a newer Windows system for a better development experience and security.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

Can visual studio code be used in python Can visual studio code be used in python Apr 15, 2025 pm 08:18 PM

VS Code can be used to write Python and provides many features that make it an ideal tool for developing Python applications. It allows users to: install Python extensions to get functions such as code completion, syntax highlighting, and debugging. Use the debugger to track code step by step, find and fix errors. Integrate Git for version control. Use code formatting tools to maintain code consistency. Use the Linting tool to spot potential problems ahead of time.

See all articles