SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
Développement web asynchrone
        avec Tornado
                Ronan Amicel
                   @amicel

      PyCon FR – Paris – 15 septembre 2012
Ronan Amicel

• Entrepreneur
• Startup advisor
• Python !
Tornado c’est quoi ?

• Un serveur web
  – scalable et non-bloquant (utilise epoll ou kqueue)

• Un framework web
  – proche de web.py ou webapp

  – exploite l’infrastructure non-bloquante sous-jacente
Bonjour les gens !
import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Bonjour les gens !")

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()
(Some) Batteries Included
•   Moteur de templates

•   Localisation

•   Client HTTP asynchrone

•   Client MySQL asynchrone

•   OpenID et OAuth (Twitter, Facebook, Google)

•   Web Socket
Cas d'utilisation
•   Seul ou en complément d'un framework « classique »

•   Sites à forte charge (milliers de requêtes / seconde)

•   Messagerie instantanée, chat rooms

•   Requêtes dépendant de services externes à latence variable
    (API Facebook, Twitter, etc.)

•   Mises à jour « temps réel » dans une page web
Mises à jour « temps réel »
         Polling                            Long Polling                                Streaming
             (Ajax)                                   (Comet)                              (Web Socket)




                            événements




                                                                      événements




                                                                                                               événements
Navigateur            Serveur            Navigateur             Serveur            Navigateur             Serveur
Web Socket
                (côté serveur)
from tornado.websocket import WebSocketHandler

class EchoWebSocket(WebSocketHandler):

   def open(self):
       print "WebSocket opened"

   def on_message(self, message):
       self.write_message(u"You said: " + message)

   def on_close(self):
       print "WebSocket closed"
Web Socket
                   (côté client)

var ws = new WebSocket("ws://localhost:8888/websocket");

ws.onopen = function () {
   ws.send("Hello, world");
};

ws.onmessage = function (evt) {
   alert(evt.data);
};
Requêtes asynchrones
from tornado.httpclient import AsyncHTTPClient
from tornado.web import RequestHandler, asynchronous

class MyRequestHandler(RequestHandler):

   @asynchronous
   def get(self):
      http = AsyncHTTPClient()
      http.fetch('http://friendfeed.com/', self._on_download)

   def _on_download(self, response):
      self.write('Downloaded!')
      self.finish()
Masquer les callbacks
          avec tornado.gen
from tornado.gen import engine, Task
from tornado.httpclient import AsyncHTTPClient
from tornado.web import RequestHandler, asynchronous


class MyRequestHandler(RequestHandler):

   @asynchronous
   @engine
   def get(self):
      http = AsyncHTTPClient()
      response = yield Task(http.fetch, 'http://friendfeed.com/')
      self.write('Downloaded!')
      self.finish()
Tâches concurrentes
          avec tornado.gen
class MyRequestHandler(RequestHandler):

   @asynchronous
   @engine
   def get(self):
      http = AsyncHTTPClient()

       response1, response2 = yield [
           Task(http.fetch, url1),
           Task(http.fetch, url2),
       ]

       self.write('Downloaded!')
       self.finish()
Bibliothèques asynchrones

•   PostgreSQL

•   MongoDB

•   Redis

•   Memcache

•   RabbitMQ
Qui utilise Tornado ?




focus.io
Historique
•   2007 : FriendFeed

•   Août 2009 : rachat par Facebook

•   Septembre 2009 : première version diffusée sous licence Apache 2.0

•   Juillet 2010 : Tornado 1.0

•   Juin 2011 : Tornado 2.0

•   Septembre 2012 : Tornado 2.4
Les côtés négatifs

• Asynchrone
 – Moins lisible que du code synchrone

 – Plus complexe à déboguer

• Communauté
 – Moins populaire que Django, Flask, Pyramid...
Ressources


•   http://www.tornadoweb.org/

•   http://pypi.python.org/pypi/tornado

•   https://github.com/facebook/tornado/

Weitere ähnliche Inhalte

Was ist angesagt?

Zabbix, garder un oeil toujours ouvert
Zabbix, garder un oeil toujours ouvertZabbix, garder un oeil toujours ouvert
Zabbix, garder un oeil toujours ouvertLook a box
 
Node.js et les nouvelles technologies javascript
Node.js et les nouvelles technologies javascriptNode.js et les nouvelles technologies javascript
Node.js et les nouvelles technologies javascriptKhalid Jebbari
 
Ze cloud azure camp - 26 septembre
Ze cloud   azure camp - 26 septembreZe cloud   azure camp - 26 septembre
Ze cloud azure camp - 26 septembreAymeric Weinbach
 
Audio procédural : la révolution WebAssembly ! par Yann ORLAREY
Audio procédural : la révolution WebAssembly ! par Yann ORLAREYAudio procédural : la révolution WebAssembly ! par Yann ORLAREY
Audio procédural : la révolution WebAssembly ! par Yann ORLAREYLa Cuisine du Web
 
Initiation à Express js
Initiation à Express jsInitiation à Express js
Initiation à Express jsAbdoulaye Dieng
 
Enib cours c.a.i. web - séance #6 : introduction à node js
Enib   cours c.a.i. web - séance #6 : introduction à node jsEnib   cours c.a.i. web - séance #6 : introduction à node js
Enib cours c.a.i. web - séance #6 : introduction à node jsHoracio Gonzalez
 
Présentation de nodejs
Présentation de nodejsPrésentation de nodejs
Présentation de nodejs13p
 
Présentation de Vagrant
Présentation de VagrantPrésentation de Vagrant
Présentation de Vagrantclmntlxndr
 

Was ist angesagt? (14)

Présentation de Node.js
Présentation de Node.jsPrésentation de Node.js
Présentation de Node.js
 
Ze cloud data-aspectize
Ze cloud   data-aspectizeZe cloud   data-aspectize
Ze cloud data-aspectize
 
Zabbix, garder un oeil toujours ouvert
Zabbix, garder un oeil toujours ouvertZabbix, garder un oeil toujours ouvert
Zabbix, garder un oeil toujours ouvert
 
Node.js et les nouvelles technologies javascript
Node.js et les nouvelles technologies javascriptNode.js et les nouvelles technologies javascript
Node.js et les nouvelles technologies javascript
 
Ze cloud azure camp - 26 septembre
Ze cloud   azure camp - 26 septembreZe cloud   azure camp - 26 septembre
Ze cloud azure camp - 26 septembre
 
Audio procédural : la révolution WebAssembly ! par Yann ORLAREY
Audio procédural : la révolution WebAssembly ! par Yann ORLAREYAudio procédural : la révolution WebAssembly ! par Yann ORLAREY
Audio procédural : la révolution WebAssembly ! par Yann ORLAREY
 
Initiation à Express js
Initiation à Express jsInitiation à Express js
Initiation à Express js
 
Enib cours c.a.i. web - séance #6 : introduction à node js
Enib   cours c.a.i. web - séance #6 : introduction à node jsEnib   cours c.a.i. web - séance #6 : introduction à node js
Enib cours c.a.i. web - séance #6 : introduction à node js
 
Vagrant - Concept
Vagrant - ConceptVagrant - Concept
Vagrant - Concept
 
Présentation de nodejs
Présentation de nodejsPrésentation de nodejs
Présentation de nodejs
 
Rust my node
Rust my nodeRust my node
Rust my node
 
Présentation de Vagrant
Présentation de VagrantPrésentation de Vagrant
Présentation de Vagrant
 
Bonnes pratiques développement android
Bonnes pratiques développement androidBonnes pratiques développement android
Bonnes pratiques développement android
 
Serveur Zabbix
Serveur ZabbixServeur Zabbix
Serveur Zabbix
 

Ähnlich wie Programmation web asynchrone avec Tornado

Java script Introduction
Java script IntroductionJava script Introduction
Java script IntroductionMohamed MHAMDI
 
[AzureCamp 24 Juin 2014] Interactions en "temps réel" pour les applications W...
[AzureCamp 24 Juin 2014] Interactions en "temps réel" pour les applications W...[AzureCamp 24 Juin 2014] Interactions en "temps réel" pour les applications W...
[AzureCamp 24 Juin 2014] Interactions en "temps réel" pour les applications W...Microsoft Technet France
 
Performance des sites web - Latence - AFUP 2010
Performance des sites web - Latence - AFUP 2010Performance des sites web - Latence - AFUP 2010
Performance des sites web - Latence - AFUP 2010Eric D.
 
Colloque cyber 2010 les botnets
Colloque cyber 2010   les botnetsColloque cyber 2010   les botnets
Colloque cyber 2010 les botnetsmichelcusin
 
Introduction aux RIA (Rich Internet Applications)
Introduction aux RIA (Rich Internet Applications)Introduction aux RIA (Rich Internet Applications)
Introduction aux RIA (Rich Internet Applications)Tugdual Grall
 
Cours 1/3 "Architecture Web"
Cours 1/3 "Architecture Web"Cours 1/3 "Architecture Web"
Cours 1/3 "Architecture Web"Adyax
 
2013 01-08-php-maturite
2013 01-08-php-maturite2013 01-08-php-maturite
2013 01-08-php-maturiteRémi Alvado
 
xml-webservices-intro.pdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
xml-webservices-intro.pdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhxml-webservices-intro.pdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
xml-webservices-intro.pdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhindguendouz2000
 
[FR] Capture vidéo avec HTML5
[FR] Capture vidéo avec HTML5[FR] Capture vidéo avec HTML5
[FR] Capture vidéo avec HTML5Thomas Bassetto
 
Une visite guidée d’Internet Explorer 9 et HTML5 pour les développeurs Web
Une visite guidée d’Internet Explorer 9 et HTML5 pour les développeurs WebUne visite guidée d’Internet Explorer 9 et HTML5 pour les développeurs Web
Une visite guidée d’Internet Explorer 9 et HTML5 pour les développeurs WebFrédéric Harper
 
Parisweb - javascript server side - par où commencer ?
Parisweb - javascript server side - par où commencer ?Parisweb - javascript server side - par où commencer ?
Parisweb - javascript server side - par où commencer ?Quentin Adam
 
Yass-RSI-Bootstrap.pptx
Yass-RSI-Bootstrap.pptxYass-RSI-Bootstrap.pptx
Yass-RSI-Bootstrap.pptxyassinesouli2
 
Node.js dans Windows Azure mobile services et web sites
Node.js dans Windows Azure mobile services et web sitesNode.js dans Windows Azure mobile services et web sites
Node.js dans Windows Azure mobile services et web sitesMicrosoft
 
Introduction dans la Programmation Web Course 1
Introduction dans la Programmation Web Course 1Introduction dans la Programmation Web Course 1
Introduction dans la Programmation Web Course 1Vlad Posea
 
Introduction aux outils_pour_le_web_-_chapitre_0_-_introduction.key
Introduction aux outils_pour_le_web_-_chapitre_0_-_introduction.keyIntroduction aux outils_pour_le_web_-_chapitre_0_-_introduction.key
Introduction aux outils_pour_le_web_-_chapitre_0_-_introduction.keyAlpha Oumar Barry
 

Ähnlich wie Programmation web asynchrone avec Tornado (20)

Java script Introduction
Java script IntroductionJava script Introduction
Java script Introduction
 
[AzureCamp 24 Juin 2014] Interactions en "temps réel" pour les applications W...
[AzureCamp 24 Juin 2014] Interactions en "temps réel" pour les applications W...[AzureCamp 24 Juin 2014] Interactions en "temps réel" pour les applications W...
[AzureCamp 24 Juin 2014] Interactions en "temps réel" pour les applications W...
 
Vert.x
Vert.xVert.x
Vert.x
 
Performance des sites web - Latence - AFUP 2010
Performance des sites web - Latence - AFUP 2010Performance des sites web - Latence - AFUP 2010
Performance des sites web - Latence - AFUP 2010
 
Support NodeJS avec TypeScript Express MongoDB
Support NodeJS avec TypeScript Express MongoDBSupport NodeJS avec TypeScript Express MongoDB
Support NodeJS avec TypeScript Express MongoDB
 
Colloque cyber 2010 les botnets
Colloque cyber 2010   les botnetsColloque cyber 2010   les botnets
Colloque cyber 2010 les botnets
 
Introduction aux RIA (Rich Internet Applications)
Introduction aux RIA (Rich Internet Applications)Introduction aux RIA (Rich Internet Applications)
Introduction aux RIA (Rich Internet Applications)
 
Cours 1/3 "Architecture Web"
Cours 1/3 "Architecture Web"Cours 1/3 "Architecture Web"
Cours 1/3 "Architecture Web"
 
2013 01-08-php-maturite
2013 01-08-php-maturite2013 01-08-php-maturite
2013 01-08-php-maturite
 
xml-webservices-intro.pdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
xml-webservices-intro.pdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhxml-webservices-intro.pdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
xml-webservices-intro.pdfhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
 
[FR] Capture vidéo avec HTML5
[FR] Capture vidéo avec HTML5[FR] Capture vidéo avec HTML5
[FR] Capture vidéo avec HTML5
 
Une visite guidée d’Internet Explorer 9 et HTML5 pour les développeurs Web
Une visite guidée d’Internet Explorer 9 et HTML5 pour les développeurs WebUne visite guidée d’Internet Explorer 9 et HTML5 pour les développeurs Web
Une visite guidée d’Internet Explorer 9 et HTML5 pour les développeurs Web
 
Parisweb - javascript server side - par où commencer ?
Parisweb - javascript server side - par où commencer ?Parisweb - javascript server side - par où commencer ?
Parisweb - javascript server side - par où commencer ?
 
Yass-RSI-Bootstrap.pptx
Yass-RSI-Bootstrap.pptxYass-RSI-Bootstrap.pptx
Yass-RSI-Bootstrap.pptx
 
Node.js dans Windows Azure mobile services et web sites
Node.js dans Windows Azure mobile services et web sitesNode.js dans Windows Azure mobile services et web sites
Node.js dans Windows Azure mobile services et web sites
 
Introduction dans la Programmation Web Course 1
Introduction dans la Programmation Web Course 1Introduction dans la Programmation Web Course 1
Introduction dans la Programmation Web Course 1
 
nodejs vs vertx
nodejs vs vertxnodejs vs vertx
nodejs vs vertx
 
Paris RailsCamp 2009
Paris RailsCamp 2009Paris RailsCamp 2009
Paris RailsCamp 2009
 
Introduction aux outils_pour_le_web_-_chapitre_0_-_introduction.key
Introduction aux outils_pour_le_web_-_chapitre_0_-_introduction.keyIntroduction aux outils_pour_le_web_-_chapitre_0_-_introduction.key
Introduction aux outils_pour_le_web_-_chapitre_0_-_introduction.key
 
Symfonytn
SymfonytnSymfonytn
Symfonytn
 

Programmation web asynchrone avec Tornado

  • 1. Développement web asynchrone avec Tornado Ronan Amicel @amicel PyCon FR – Paris – 15 septembre 2012
  • 2. Ronan Amicel • Entrepreneur • Startup advisor • Python !
  • 3. Tornado c’est quoi ? • Un serveur web – scalable et non-bloquant (utilise epoll ou kqueue) • Un framework web – proche de web.py ou webapp – exploite l’infrastructure non-bloquante sous-jacente
  • 4. Bonjour les gens ! import tornado.ioloop import tornado.web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Bonjour les gens !") application = tornado.web.Application([ (r"/", MainHandler), ]) if __name__ == "__main__": application.listen(8888) tornado.ioloop.IOLoop.instance().start()
  • 5. (Some) Batteries Included • Moteur de templates • Localisation • Client HTTP asynchrone • Client MySQL asynchrone • OpenID et OAuth (Twitter, Facebook, Google) • Web Socket
  • 6. Cas d'utilisation • Seul ou en complément d'un framework « classique » • Sites à forte charge (milliers de requêtes / seconde) • Messagerie instantanée, chat rooms • Requêtes dépendant de services externes à latence variable (API Facebook, Twitter, etc.) • Mises à jour « temps réel » dans une page web
  • 7. Mises à jour « temps réel » Polling Long Polling Streaming (Ajax) (Comet) (Web Socket) événements événements événements Navigateur Serveur Navigateur Serveur Navigateur Serveur
  • 8. Web Socket (côté serveur) from tornado.websocket import WebSocketHandler class EchoWebSocket(WebSocketHandler): def open(self): print "WebSocket opened" def on_message(self, message): self.write_message(u"You said: " + message) def on_close(self): print "WebSocket closed"
  • 9. Web Socket (côté client) var ws = new WebSocket("ws://localhost:8888/websocket"); ws.onopen = function () { ws.send("Hello, world"); }; ws.onmessage = function (evt) { alert(evt.data); };
  • 10. Requêtes asynchrones from tornado.httpclient import AsyncHTTPClient from tornado.web import RequestHandler, asynchronous class MyRequestHandler(RequestHandler): @asynchronous def get(self): http = AsyncHTTPClient() http.fetch('http://friendfeed.com/', self._on_download) def _on_download(self, response): self.write('Downloaded!') self.finish()
  • 11. Masquer les callbacks avec tornado.gen from tornado.gen import engine, Task from tornado.httpclient import AsyncHTTPClient from tornado.web import RequestHandler, asynchronous class MyRequestHandler(RequestHandler): @asynchronous @engine def get(self): http = AsyncHTTPClient() response = yield Task(http.fetch, 'http://friendfeed.com/') self.write('Downloaded!') self.finish()
  • 12. Tâches concurrentes avec tornado.gen class MyRequestHandler(RequestHandler): @asynchronous @engine def get(self): http = AsyncHTTPClient() response1, response2 = yield [ Task(http.fetch, url1), Task(http.fetch, url2), ] self.write('Downloaded!') self.finish()
  • 13. Bibliothèques asynchrones • PostgreSQL • MongoDB • Redis • Memcache • RabbitMQ
  • 14. Qui utilise Tornado ? focus.io
  • 15. Historique • 2007 : FriendFeed • Août 2009 : rachat par Facebook • Septembre 2009 : première version diffusée sous licence Apache 2.0 • Juillet 2010 : Tornado 1.0 • Juin 2011 : Tornado 2.0 • Septembre 2012 : Tornado 2.4
  • 16. Les côtés négatifs • Asynchrone – Moins lisible que du code synchrone – Plus complexe à déboguer • Communauté – Moins populaire que Django, Flask, Pyramid...
  • 17. Ressources • http://www.tornadoweb.org/ • http://pypi.python.org/pypi/tornado • https://github.com/facebook/tornado/