36
WSGI, Django & Gunicorn Benoît Chesneau 25/04/2010 - djangocong Sunday, April 25, 2010

WSGI, Django, Gunicorn

Embed Size (px)

DESCRIPTION

Presentation given at #djangocong

Citation preview

Page 1: WSGI, Django, Gunicorn

WSGI, Django & GunicornBenoît Chesneau

25/04/2010 - djangocong

Sunday, April 25, 2010

Page 2: WSGI, Django, Gunicorn

[email protected] webMinimal web & OpensourceEnki Multimediahttp://www.e-engura.com

benoît chesneau

Sunday, April 25, 2010

Page 3: WSGI, Django, Gunicorn

• WSGI ?

• Django & WSGI

• Gunicorn

Sunday, April 25, 2010

Page 4: WSGI, Django, Gunicorn

WSGI ?

• Web Server Gateway Interface

• PEP 333

• Interface entre le web et une application Python

Sunday, April 25, 2010

Page 5: WSGI, Django, Gunicorn

Serveur web

WSGI

Application Python

Sunday, April 25, 2010

Page 6: WSGI, Django, Gunicorn

def app(environ, start_response): """Simplest possible application object""" data = 'Hello, World!\n' status = '200 OK' response_headers = [ ('Content-type','text/plain'), ('Content-Length', str(len(data))) ] start_response(status, response_headers) return iter([data])

simple application

Sunday, April 25, 2010

Page 7: WSGI, Django, Gunicorn

WSGI ?

• Réutilisation d’applications

• Middleware

• Paster

• Frameworks: webob, werkzeug, bottle, repoze, ...

Sunday, April 25, 2010

Page 8: WSGI, Django, Gunicorn

Middleware WSGI

class CustomHeader(object): def __init__(self, app): self.app = app def __call__(self, environ, start_response): environ["HTTP_X_MY_HEADER"] = "1" return self.app(environ, start_response)

Sunday, April 25, 2010

Page 9: WSGI, Django, Gunicorn

Serveur web

WSGI

Application Python

App App App

Sunday, April 25, 2010

Page 10: WSGI, Django, Gunicorn

Django & WSGI

Sunday, April 25, 2010

Page 11: WSGI, Django, Gunicorn

Intégrer

• Pourquoi réinventer la roue ?

• Principes communs: middlewares, request, ...

• Réutiliser les applications.

• intégrer != compatible wsgi

Sunday, April 25, 2010

Page 12: WSGI, Django, Gunicorn

• django-wsgi

• twod.wsgi

Sunday, April 25, 2010

Page 13: WSGI, Django, Gunicorn

django-wsgi

• pas de dependance

• permet d’embarquer des apps wsgi, `django_view`

• permet de creer une app wsgi à partir d’une vue ou des urls, `wsgi_application`

Sunday, April 25, 2010

Page 14: WSGI, Django, Gunicorn

django_viewdef test_app(environ, start_response): start_response("200 OK", [("Content-type", "text/html")]) yield "i suck"

def test_app1(request): return HttpResponse("wowa, meta")

urls = patterns("", .. (r"^test/$", django_view(test_app)), (r"^(?P<name>.*?)/$", test_app1), ..)

application = wsgi_application(urls)

Sunday, April 25, 2010

Page 15: WSGI, Django, Gunicorn

• vise à créer une "paserelle coopérative" entre Django et WSGI

• basé sur webob (wrap HttpRequest et HttpResponse)

• Paste deploy factory

• Middleware pour le routage

twod.wsgi

Sunday, April 25, 2010

Page 16: WSGI, Django, Gunicorn

twod.wsgi - embed

import osfrom twod.wsgi import DjangoApplication

os.environ['DJANGO_SETTINGS_MODULE'] = "yourpackage.settings"django_app = DjangoApplication()

Sunday, April 25, 2010

Page 17: WSGI, Django, Gunicorn

twod.wsgi routing args

(r'^/blog/posts/(?<post_slug>\w+)/comments/(?<post_comment_id>\d+)$'

>>> request.urlvars{'post_slug': "hello-world", 'post_comment_id': "3"}

Sunday, April 25, 2010

Page 18: WSGI, Django, Gunicorn

twod.wsgi - embed

• intégrer une application WSGI au sein de votre projet django

• modifier les requêtes/réponses

• Integrer votre app django dans une app WSGI

Sunday, April 25, 2010

Page 19: WSGI, Django, Gunicorn

embarque les apps WSGI

from twod.wsgi import call_wsgi_appfrom somewhere import wsgi_app

def run_app(request, path_info): response = call_wsgi_app(wsgi_app, request, path_info) response['Server'] = "twod.wsgi 1.0" return response

Sunday, April 25, 2010

Page 20: WSGI, Django, Gunicorn

Déployer

• l’utilisation courante de Django avec WSGI

• 2 types de deploiements :

• via serveur

• via proxy

Sunday, April 25, 2010

Page 21: WSGI, Django, Gunicorn

Serveur

• uWSGI

• mod_proxy

Sunday, April 25, 2010

Page 22: WSGI, Django, Gunicorn

proxy

• spawning, paster, ...

• cherrypy, ..

• gunicorn

Sunday, April 25, 2010

Page 23: WSGI, Django, Gunicorn

Sunday, April 25, 2010

Page 24: WSGI, Django, Gunicorn

Sunday, April 25, 2010

Page 25: WSGI, Django, Gunicorn

gunicorn

• Green unicorn

• WSGI 1.0, clients lents et rapides

• supporte WSGI, Paster compatible app & ... Django

• Load balancing via pre-fork and un socket partagé

• Upgrade à chaud “à la nginx”

Sunday, April 25, 2010

Page 26: WSGI, Django, Gunicorn

mais encore...

• HTTP Stream. Décode le HTTP à la volée

• Gestion des connexions asynchrones (longpolling, comet, websockets, appels d’api, ...).

• Eventlet, Gevent, Tornado

• DSL Config

Sunday, April 25, 2010

Page 27: WSGI, Django, Gunicorn

Philosophie

• Simple

• Minimal

• Performant

• Unix

Sunday, April 25, 2010

Page 28: WSGI, Django, Gunicorn

Simple

• gunicorn_django -w 3 /myproject/settings.py

• ./manage.py run_django -w 3

• gunicorn_django -w 3 -k “egg:gunicorn#eventlet” /myproject/settings.py

Sunday, April 25, 2010

Page 29: WSGI, Django, Gunicorn

Simple

• Taille memoire controllée

• Derrière NGINX

• Full Python

• Graceful Reload

Sunday, April 25, 2010

Page 30: WSGI, Django, Gunicorn

http://www.peterbe.com/plog/fcgi-vs-gunicorn-vs-uwsgi

gunicorn is the winner in my eyes. It's easy to configure and get up and running and certainly fast enough [..] .

Sunday, April 25, 2010

Page 31: WSGI, Django, Gunicorn

DEMO

Sunday, April 25, 2010

Page 32: WSGI, Django, Gunicorn

0.9

• Parseur HTTP en C (fallback en python sur les plateformes non supportées)

• Increase unitests

• Reload hook

• status ?

Sunday, April 25, 2010

Page 33: WSGI, Django, Gunicorn

Liens

• http://gunicorn.org

• http://e-engura.org

• http://www.python.org/dev/peps/pep-0333/

• http://bitbucket.org/2degrees/twod.wsgi/

• http://github.com/alex/django-wsgi

Sunday, April 25, 2010

Page 34: WSGI, Django, Gunicorn

Questions

Sunday, April 25, 2010

Page 35: WSGI, Django, Gunicorn

@benoitc

Sunday, April 25, 2010

Page 36: WSGI, Django, Gunicorn

Cette création est mise à disposition selon le Contrat Paternité 2.0 France disponible en ligne http://

creativecommons.org/licenses/by/2.0/fr/ ou par courrier postal à Creative Commons, 171 Second Street, Suite

300, San Francisco, California 94105, USA.

Sunday, April 25, 2010