开发者

Why should I use WSGI?

开发者 https://www.devze.com 2022-12-12 14:34 出处:网络
Been using mod_python for a while, I read more and more articles about how good WSGI is, without really understanding why.

Been using mod_python for a while, I read more and more articles about how good WSGI is, without really understanding why.

So why should I switch to it? 开发者_StackOverflow中文版What are the benefits? Is it hard, and is the learning curve worth it?


For developing sophisticated web applications in Python, you would probably use a more comprehensive web development framework like DJango, Zope, Turbogears etc. As an application developer, you don't have to worry about WSGI much. All you have to be aware about is that these frameworks support WSGI. The WSGI allows separation of web server and web application code and a system administrator can change the web server as long as the web application is WSGI compliant. If you are developing in one of these frameworks, you would anyway be satisfying this condition.

If you are a web framework developer (that is developing DJango or Zope itself), then you have to understand WSGI in more depth.


mod_wsgi vs. mod_python:

  • mod_wsgi is a little faster (internally there's more C, less Python)
  • mod_wsgi processes can be isolated from Apache, which improves security/stability with lower memory use[1]
  • mod_python gives you access to some of Apache's internals

WSGI in general:

  • lots of reusable middleware (authentication/authorisation, session stuff, caching, filtering)
  • ease of deployment on non-Apache webservers either via native WSGI support or flup

[1] - compared to a preforking Apache, which maintains a separate Python interpreter in each process


So why should I switch to it? What are the benefits?

Usually, if you have a Web Server like NGINX or Apache, you have to enable modules (although the configuration of modules in both cases are different).

WSGI is a standard described on PEP 3333 and basically, provides a standard interface between web applications written in Python and Webservers.

That means, WSGI gives portability to your Python Web Application across many different Web Servers, without any additional configurations on your NGINX, Apache, etc.

Besides that, a WSGI server can give you a lot of functionalities with more flexibility, in comparison with a Web Server. Gunicorn, provides a lot of features like:

  • Number of worker threads for handling requests
  • Maximum number of simultaneous clients.
  • Maximum number of pending connections.
  • Limit the allowed size of an HTTP request header field.
  • Maximum number of requests a worker will process before restarting.

Here is a complete document about the options supported by Gunicorn.


Is it hard, and is the learning curve worth it?

As Software Developer, you don't need to understand every detail about the standard but as System Administrator, you may need to understand a little bit more, than just doing pip install gunicorn and so on.

References

  • PEP 3333 (Python Web Server Gateway Interface)
  • WSGI.org (What is WSGI?)


Most Python frameworks implement wsgi. There is mod_wsgi for apache and a SCGI/FastCGI/AJP module + Flup for the others. That way you can have all the advantages of a separate Python process, without being tied to one webserver.


You shouldn't have to relearn much, since the difference from a developer perspective is just a small wrapper and some server configuration.

From a deployment perspective, the difference is that your python code lives in a separate process from the web browser, which means

a) The python process can be running as another user than the web server. This can be valuable for security, if used right.

b) The web server processes does not need to contain the python runtime. This can be a major boost for performance if the server runs a lot of "other" requests (static files, etc) and some heavy python requests.


WSGI is the standard API, which enables you to choose the webserver, and also put A WSGI pipeline such as Repoze in front of it.

See http://repoze.org/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号