SlideShare a Scribd company logo
WSGI, Django &
                           Gunicorn
                              Benoît Chesneau
                          25/04/2010 - djangocong
Sunday, April 25, 2010
benoît chesneau

              benoitc@apache.org
              Artisan web
              Minimal web & Opensource
              Enki Multimedia
              http://www.e-engura.com



Sunday, April 25, 2010
• WSGI ?
                         • Django & WSGI
                         • Gunicorn




Sunday, April 25, 2010
WSGI ?

                     • Web Server Gateway Interface
                     • PEP 333
                     • Interface entre le web et une application
                         Python




Sunday, April 25, 2010
Serveur web




                               WSGI




                         Application Python




Sunday, April 25, 2010
simple application
                   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])




Sunday, April 25, 2010
WSGI ?

                     • Réutilisation d’applications
                     • Middleware
                     • Paster
                     • Frameworks: webob, werkzeug, bottle,
                         repoze, ...



Sunday, April 25, 2010
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
Serveur web




                                 WSGI




                          Application Python


                         App      App        App


Sunday, April 25, 2010
Django & WSGI



Sunday, April 25, 2010
Intégrer

                     • Pourquoi réinventer la roue ?
                     • Principes communs: middlewares,
                         request, ...
                     • Réutiliser les applications.
                     • intégrer != compatible wsgi

Sunday, April 25, 2010
• django-wsgi
                     • twod.wsgi



Sunday, April 25, 2010
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
django_view
                 def 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
twod.wsgi
                     • 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

Sunday, April 25, 2010
twod.wsgi - embed

          import os
          from twod.wsgi import DjangoApplication

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




Sunday, April 25, 2010
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
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
embarque les apps
                              WSGI
           from twod.wsgi import call_wsgi_app
           from 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
Déployer

                     • l’utilisation courante de Django avec WSGI
                     • 2 types de deploiements :
                      • via serveur
                      • via proxy

Sunday, April 25, 2010
Serveur


                     • uWSGI
                     • mod_proxy


Sunday, April 25, 2010
proxy

                     • spawning, paster, ...
                     • cherrypy, ..
                     • gunicorn


Sunday, April 25, 2010
Sunday, April 25, 2010
Sunday, April 25, 2010
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
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
Philosophie

                     • Simple
                     • Minimal
                     • Performant
                     • Unix

Sunday, April 25, 2010
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
Simple

                     • Taille memoire controllée
                     • Derrière NGINX
                     • Full Python
                     • Graceful Reload

Sunday, April 25, 2010
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
DEMO



Sunday, April 25, 2010
0.9

                     • Parseur HTTP en C (fallback en python sur
                         les plateformes non supportées)
                     • Increase unitests
                     • Reload hook
                     • status ?

Sunday, April 25, 2010
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
Questions




Sunday, April 25, 2010
@benoitc




Sunday, April 25, 2010
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

More Related Content

PDF
Large problems, Mostly Solved
PDF
Couchdbkit djangocong-20100425
PDF
Lecture 10 slides (february 4, 2010)
PDF
HTML 5: The Future of the Web
PDF
MySQL Sandbox - A toolkit for laziness
PDF
Scaling webappswithrabbitmq
PDF
NodeJS, CoffeeScript & Real-time Web
PDF
iPhone Memory Management
Large problems, Mostly Solved
Couchdbkit djangocong-20100425
Lecture 10 slides (february 4, 2010)
HTML 5: The Future of the Web
MySQL Sandbox - A toolkit for laziness
Scaling webappswithrabbitmq
NodeJS, CoffeeScript & Real-time Web
iPhone Memory Management

Viewers also liked (20)

KEY
PyCon US 2012 - State of WSGI 2
PDF
A look at FastCgi & Mod_PHP architecture
KEY
Large platform architecture in (mostly) perl - an illustrated tour
PPT
A Brief Introduce to WSGI
KEY
Intro to PSGI and Plack
PDF
Scaling Django with gevent
PDF
Djangoのエントリポイントとアプリケーションの仕組み
PPT
Common gateway interface
PPT
Common Gateway Interface
PDF
FCGI, C++로 Restful 서버 개발
PDF
Articulo Malditas Tarjetas Eq3
PDF
插图伊斯兰教简明指南 _ Chinese
PDF
Vrouwen Binnen De Islaam
PDF
Monthly seasonal outlook
PPT
Budgeting and Savings
PDF
Writing winning proposals
PPT
Let’S Go To The Beach
PDF
Couchdbkit & Dango
PDF
Merle Norman By Epic
PPT
Presentació assignatura informà 1er eso
PyCon US 2012 - State of WSGI 2
A look at FastCgi & Mod_PHP architecture
Large platform architecture in (mostly) perl - an illustrated tour
A Brief Introduce to WSGI
Intro to PSGI and Plack
Scaling Django with gevent
Djangoのエントリポイントとアプリケーションの仕組み
Common gateway interface
Common Gateway Interface
FCGI, C++로 Restful 서버 개발
Articulo Malditas Tarjetas Eq3
插图伊斯兰教简明指南 _ Chinese
Vrouwen Binnen De Islaam
Monthly seasonal outlook
Budgeting and Savings
Writing winning proposals
Let’S Go To The Beach
Couchdbkit & Dango
Merle Norman By Epic
Presentació assignatura informà 1er eso
Ad

Similar to WSGI, Django, Gunicorn (20)

PDF
Behind the curtain - How Django handles a request
PDF
Python WSGI introduction
PDF
Introduction To Google App Engine
PDF
Docs Python Org Howto Webservers Html
KEY
Plack at YAPC::NA 2010
PPTX
Python wsgi protocol
KEY
Deploying Plack Web Applications: OSCON 2011
ODP
dJango
PDF
Jsp & Ajax
PDF
Batteries not included
PDF
2015-10-07 PPDC HTTP Adapters
KEY
Java web programming
KEY
Plack at OSCON 2010
PPTX
Servletarchitecture,lifecycle,get,post
PPTX
Servletarchitecture,lifecycle,get,post
PDF
High Availability Server Apps
PPTX
Servletarchitecture,lifecycle,get,post
PDF
Progressive Enhancement using WSGI
ODP
Configure python and wsgi
KEY
Deploying
Behind the curtain - How Django handles a request
Python WSGI introduction
Introduction To Google App Engine
Docs Python Org Howto Webservers Html
Plack at YAPC::NA 2010
Python wsgi protocol
Deploying Plack Web Applications: OSCON 2011
dJango
Jsp & Ajax
Batteries not included
2015-10-07 PPDC HTTP Adapters
Java web programming
Plack at OSCON 2010
Servletarchitecture,lifecycle,get,post
Servletarchitecture,lifecycle,get,post
High Availability Server Apps
Servletarchitecture,lifecycle,get,post
Progressive Enhancement using WSGI
Configure python and wsgi
Deploying
Ad

Recently uploaded (20)

PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPT
Teaching material agriculture food technology
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Electronic commerce courselecture one. Pdf
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PPTX
Machine Learning_overview_presentation.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PPTX
A Presentation on Artificial Intelligence
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Teaching material agriculture food technology
Diabetes mellitus diagnosis method based random forest with bat algorithm
Advanced methodologies resolving dimensionality complications for autism neur...
Network Security Unit 5.pdf for BCA BBA.
Electronic commerce courselecture one. Pdf
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Machine Learning_overview_presentation.pptx
Encapsulation_ Review paper, used for researhc scholars
MYSQL Presentation for SQL database connectivity
Per capita expenditure prediction using model stacking based on satellite ima...
The AUB Centre for AI in Media Proposal.docx
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Assigned Numbers - 2025 - Bluetooth® Document
A Presentation on Artificial Intelligence
20250228 LYD VKU AI Blended-Learning.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Building Integrated photovoltaic BIPV_UPV.pdf

WSGI, Django, Gunicorn

  • 1. WSGI, Django & Gunicorn Benoît Chesneau 25/04/2010 - djangocong Sunday, April 25, 2010
  • 2. benoît chesneau benoitc@apache.org Artisan web Minimal web & Opensource Enki Multimedia http://www.e-engura.com Sunday, April 25, 2010
  • 3. • WSGI ? • Django & WSGI • Gunicorn Sunday, April 25, 2010
  • 4. WSGI ? • Web Server Gateway Interface • PEP 333 • Interface entre le web et une application Python Sunday, April 25, 2010
  • 5. Serveur web WSGI Application Python Sunday, April 25, 2010
  • 6. simple application 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]) Sunday, April 25, 2010
  • 7. WSGI ? • Réutilisation d’applications • Middleware • Paster • Frameworks: webob, werkzeug, bottle, repoze, ... Sunday, April 25, 2010
  • 8. 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
  • 9. Serveur web WSGI Application Python App App App Sunday, April 25, 2010
  • 10. Django & WSGI Sunday, April 25, 2010
  • 11. Intégrer • Pourquoi réinventer la roue ? • Principes communs: middlewares, request, ... • Réutiliser les applications. • intégrer != compatible wsgi Sunday, April 25, 2010
  • 12. • django-wsgi • twod.wsgi Sunday, April 25, 2010
  • 13. 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
  • 14. django_view def 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
  • 15. twod.wsgi • 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 Sunday, April 25, 2010
  • 16. twod.wsgi - embed import os from twod.wsgi import DjangoApplication os.environ['DJANGO_SETTINGS_MODULE'] = "yourpackage.settings" django_app = DjangoApplication() Sunday, April 25, 2010
  • 17. 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
  • 18. 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
  • 19. embarque les apps WSGI from twod.wsgi import call_wsgi_app from 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
  • 20. Déployer • l’utilisation courante de Django avec WSGI • 2 types de deploiements : • via serveur • via proxy Sunday, April 25, 2010
  • 21. Serveur • uWSGI • mod_proxy Sunday, April 25, 2010
  • 22. proxy • spawning, paster, ... • cherrypy, .. • gunicorn Sunday, April 25, 2010
  • 25. 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
  • 26. 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
  • 27. Philosophie • Simple • Minimal • Performant • Unix Sunday, April 25, 2010
  • 28. 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
  • 29. Simple • Taille memoire controllée • Derrière NGINX • Full Python • Graceful Reload Sunday, April 25, 2010
  • 30. 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
  • 32. 0.9 • Parseur HTTP en C (fallback en python sur les plateformes non supportées) • Increase unitests • Reload hook • status ? Sunday, April 25, 2010
  • 33. 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
  • 36. 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