基于Python Web应用的Web服务器之间的比较

介绍

  • 在这篇文章中,我们将主要讨论三件事情:Python, Web Servers, 还有最重要的一点是讨论这两者之间一些七零八碎的关系。

  • 撇开笑话不谈,这篇相当长的文章对于那些正在找寻快捷的指导方法或者答案的人来说可能是不合时宜的。不幸的是,不像Python世界中的大多数事情那样,当谈到选择一个产品服务栈来部署你的应用时,这里没有一个明显的方法来教你怎样做或者说哪一个更合适。

  • 值得庆幸的是,这不应该吓到你。当你读完这篇文章,你将对Web Servers 如何工作和处理基于Python Web应用之间的会话任务的不同之处有一个更好的认识。最后根据你的需要和要求,你将可以决定哪个Server更适合你。

Python Web Server Gateway Interface v1.0(WSGI:Python Web服务网关接口)

  • 理解问题之所在
    今天,越来越多的Web Servers被设计出来与Python Web应用一起工作,而且这些不同的Web servers是可以互相替换的。然而,这样的情况却不是一直这样的。在早得时候,开发者们几乎不可能随心所欲的更换Web Servers,而且每次更换都会因为各种依赖和限制因素造成很大的开销。当你决定选择某一个框架的时候,虽然你不一定总是乐意,但是你不得不同时考虑在服务端你有哪些选择,可以用来为应用程序服务。这是因为缺少一个被普遍接受的接口规范:一种共识,应用程序(框架)和Web Servers之间可以互相适应和互相交流,允许在必要的时候尽可能做到代码零改变的交换相应的组件。

  • 标准的诞生
    在本世纪初,Python 社区在做了很多努力后终于解决了这个问题,描述在PEP 333中。来自PEP 333:

这个文档指定了一个在web servers 和 Python web应用程序或者框架之间被推荐使用的标准接口,用来提升web应用程序在各种web servers之间的可移植性。
  • PEP中的解释是,这个新的标准意味着允许在web servers 和Python web应用程序之间的移植。这个标准的强大特性和它的广泛采纳与以前的那些比起来为现在提供了很好的示范作用:这个世界上有很多的web servers 乐意为你做这些工作。

Web Servers之间的比较

  • 在这个基于Python web应用程序的web servers之间的比较中,我们将讨论一些有效的选择和这些web servers分别具有什么样的优势。在这里的主要目的是让读者有一个更清晰的认识,为读者提供一些帮助,能根据自己应用程序的相应需求选择一个更合适的server。因为当前对于server有着大量的选择,我们将通过一些方法来过滤某些选择,然后讨论这些server在以下这几个方面都有哪些特别的地方:受欢迎程度,健壮程度,或者哪个server具有其他server所不具备的特性。

下面只是选择翻译了一些实际应用中用的比较多的。更多内容可以参考原文。

Gunicorn

-What is it?

  • Gunicorn is a stand-alone web server which offers quite a bit of functionality in a significantly easy to operate fashion. It uses the pre-fork model – meaning that a central master process (Gunicorn) is tasked with managing the initiated worker processes (of differing types), which then handle and deal with the requests directly. And all this can be configured and adapted to suit your needs and diverse production scenarios.

-Why should you consider using it?

  • It supports WSGI and can be used with any WSGI running Python application and framework.
  • It can also be used as a drop-in replacement for Paster (ex: Pyramid), Django’s Development Server, web2py, et alia.
  • Offers the choice of various worker types/configurations and automatic worker process management.
  • HTTP/1.0 and HTTP/1.1 (Keep-Alive) support through synchronous and asynchronous workers.
  • Comes with SSL support
  • Extensible with hooks.
  • It is transparent and has a clear architecture.
  • Supports Python versions 2.x >= 2.6 or 3.x >= 3.2

uWSGI

-What is it?

  • Despite its very confusing naming conventions, uWSGI itself is a vast project with many components, aiming to provide a full [software] stack for building hosting services. One of these components, the uWSGI server, runs Python WSGI applications. It is capable of using various protocols, including its own uwsgi wire protocol, which is quasi-identical to SCGI. In order to fulfil the (understandable) demand to use stand-alone HTTP servers in front of application servers, NGINX and Cherokee web servers are modularised to support uWSGI’s (best performing) uwsgi protocol to have direct control over its processes.

-Why should you consider using it?

  • uWSGI comes with a WSGI adapter and it fully supports Python applications running on WSGI.
  • It links with libpython. It loads the application code on startup and acts like a Python interpreter. It parses the incoming requests and invokes the Python callable.
  • It comes with direct support for popular NGINX web server (along with Cherokee* and lighttpd).
  • It is written in C.
  • Its various components can do much more than running an application, which might be handy for expansion.
  • Currently (as of late 2013), it is actively developed and has fast release cycles.
  • It has various engines for running applications (asynchronous and synchronous).
  • It can mean lower memory footprint to run.

Tornado (HTTP Server via wsgi.WSGIContainer)

-What is it?

  • Tornado is an application development framework and a networking library designed for handling asynchrnous operations, allowing servers to maintain a lot of open connections. It also comes with a WSGI server which other WSGI Python applications (and frameworks) can use to run.

-Why should you consider using it?

  • If you are building on top Tornado framework; or
    Your application needs asynchronous functionality.
    Although under these circumstances you might want to choose Tornado’s WSGI server for your project, you can also opt to use Gunicorn with Tornado [Asynchronous] workers.

原文链接:A Comparison of Web Servers for Python Based Web Applications

你可能感兴趣的:(python,web服务器,tornado,uwsgi,gunicorn)