Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Tornado in Depth

1,616 views

Published on

Tornado is a non-blocking light-weight web server and framework. There's been many introductory talks about it, and it's time to look deeper into it: not just what Tornado does, but how it does it and what can we learn from it when designing our own concurrent systems.

In this talk I go over the following topics. I cover them in two parts: first I present how to use a certain feature or approach in our applications; then, I dig into Tornado's source code to see how it really works.

- Getting Started: quickly get a simple Tornado application up and running. We'll keep digging into, changing and poking this Application for most of the talk.

- An Application Listens: what an Application is, how does Tornado start it and how does it process its requests.

- Application and IOLoop: we'll look at how the IOLoop receives the connections from the users and passes them on to the Applications.

- Scheduled Tasks: we'll see how to schedule tasks and how the IOLoop will run them.

- Generators: we'll learn to use generators to handle the responses of our asynchronous calls, and how they work with the IOLoop.

Advanced:

- Websockets: how to use them and how they work.

- IOStream: how do Tornado's non-blocking sockets work.

- Database: how to use non-blocking sockets to connect to databases.

- Process: how Tornado works with multiple processes.

I presented this talk at Europython 2012 and PyGrunn 2012.

Code examples: https://bitbucket.org/grimborg/tornado-in-depth/src/tip/examples/

Published in: Technology, Business

Tornado in Depth

  1. 1. TORNADO IN DEPTH
  2. 2. thor
  3. 3. thunder
  4. 4. tro
  5. 5. tronar
  6. 6. tronada
  7. 7. tornar
  8. 8. tornada
  9. 9. tornada
  10. 10. tronada + tornada
  11. 11. TORNADO
  12. 12. what is it? Non-blocking web server Based on epoll / kqueue Handles 1000s of connections Powers FriendFeed
  13. 13. why do you even care? Know what you use I can't trust what I don't know Learning and sharing: fun!
  14. 14. what do you use it for? We built a Scalable Distributed Fault tolerant High load ...thing
  15. 15. [CITATION NEEDED]
  16. 16. oscar.vilaplana@paylogic.eu
  17. 17. TORNADO
  18. 18. what is it made of? IOLoop Callbacks, Tasks, Timeouts TCPServer Application RequestHandler Generators
  19. 19. can I extend it? Sure! It's easy How would we make a simple TCPServer?
  20. 20. 0b_tcpserver.py show me the source!
  21. 21. GETTING STARTED
  22. 22. how do I make the simplest app? Define a RequestHandler Implement get Define an Application Tell the application to listen Start the IOLoop
  23. 23. 01_getting_started.py show me the source!
  24. 24. what is an Application? Collection of RequestHandlers Implements listen: Starts an HTTPServer Sets the Application as request callback Implements __call__: Handles the user requests
  25. 25. t1_application_listen.py show me the source!
  26. 26. what does Application.__call__ do? Parses the URL Decides which handler to use and creates an instance of it (each connection gets one) _executes it (passing any defined transforms to it –e.g, Gzip compression)
  27. 27. t2_application_call.py show me the source!
  28. 28. what does RequestHandler._execute do? Calls the handler method Checks XSRF cookie Maybe closes the connection
  29. 29. t3_request_handler_ _execute.py show me the source!
  30. 30. IOLOOP CALLBACK TIMEOUT event
  31. 31. Core of Tornado Usable standalone or with WSGI Used for server and client Single instance per process Small! what is the IOLoop?
  32. 32. Loops forever Executes: Callbacks (asap) Timeouts (when due) Events (when occoured) what does the IOLoop do?
  33. 33. what is an Event? Something that happened on a socket (fd) (e.g. a user opened a conneciton) Applications define handlers for Events
  34. 34. how do I wait for an Event? add_handler(fd, handler, events) update_handler(fd, handler, events) remove_handler(fd, handler) “Notify me when I can READ or WRITE, or when there is an ERROR”
  35. 35. IOLOOP THE LOOP in four steps
  36. 36. Process Callbacks They were scheduled by previous: Callbacks Event handlers first process the Callbacks
  37. 37. For each Timeout: Is it due now? Run its callback Calculate the time till next Timeout second process the Timeouts
  38. 38. Poll timeout: Callbacks? Then 0 Timeouts? Then time until the next Timeout Neither? 1 hour Here the IOLoop blocks third poll for Events
  39. 39. For each (file descriptor, Event): Call its handler ...and repeat the loop and fourth process the Events
  40. 40. THE EXAMPLE WHAT it really does
  41. 41. 01_getting_started.py show me the source!
  42. 42. Application.listen starts HTTPServer calls IOLoop.add_accept_handler (Application.__call__ will handle the ACCEPT event) and when a client connects... what does the example do?
  43. 43. what does the example do? IOLoop polls connection TCPServer calls HTTPServer which is HTTPConnection creates read and callsheaders body reads using callbacks request_callback and calls Application.__call__ which is RequestHandler ._execute calls (after parsing URL) RequestHandler calls get or post or ... calls HTTPConnection writes to (in chunks) _auto_finish? finishes yes
  44. 44. t4_simple_ioloop.py show me the source!
  45. 45. Scheduled tasks
  46. 46. how do we schedule a task? Call me back asap Ioloop.add_callback Call me back later Ioloop.add_timeout Call me often PeriodicCallback
  47. 47. 02_scheduled_tasks.py show me the source!
  48. 48. how does add_callback work? Adds the callback to the list of callbacks. (and wraps it in a threadlocal-like stack context)
  49. 49. what do you mean, threadlocal-like? StackContext Keeps track of the socket connection Handles association between socket and Application classes
  50. 50. t6_add_callback.py show me the source!
  51. 51. how does add_timeout work? Pushes the timeout to the heap of timeouts. (and wraps it in a threadlocal-like stack context too)
  52. 52. t7_add_timeout.py show me the source!
  53. 53. how do PeriodicCallbacks work? start Schedules the next timeout to call _run Marks the PeriodicCallback as running stop Removes the next timeout Marks the PeriodicCallback as stopped _run Calls the callback (unless stop was called)
  54. 54. t8_periodic_callback.py show me the source!
  55. 55. what about Callback? Indeed.
  56. 56. Async handlers asynchronous@ & auto_ _finish
  57. 57. how does @asynchronous work? Sets _auto_finish to False (and does some Exception wrapping) The connection remains open after get, post... Close it yourself (whenever you want)
  58. 58. 03_fetch_async.py show me the source!
  59. 59. I put a callback on your callback Nested callbacks make ugly code.
  60. 60. what about Callback? Indeed.
  61. 61. generators
  62. 62. how do I avoid callbacks? Use Callback (finally!) and yield Or Task
  63. 63. 04_fetch_gen.py show me the source!
  64. 64. Yield points
  65. 65. what is a YieldPoint? Something you yield Then stuff happens
  66. 66. what is a YieldPoint? Callback Sends a result for a key Wait Waits till a result for a key arrives WaitMany Same as Wait, for many keys
  67. 67. Task Wait + Callback (with an auto-generated key) Multi List of YeldPoints what is a YieldPoint?
  68. 68. how do we do async processing? callback=(yield Callback(“key”)) When a result arrives, send it for the key “key”
  69. 69. how do we do async processing? response=yield Wait(“key”) When the result is sent, read it into response.
  70. 70. 04_fetch_gen.py show me the source!
  71. 71. 05_task.py show me the source!
  72. 72. t9_yield_points.py show me the source!
  73. 73. websockets
  74. 74. how do we use websockets? Extend WebSocketHandler (instead of RequestHandler) Implement on_message
  75. 75. 06_websocket.py show me the source!
  76. 76. how do websockets work? Similar to @asynchronous (the connection is kept open) After writing, read for more (asynchronously—see IOStream) To Application, it looks like a RequestHandler
  77. 77. how does WebSocket work? _execute Accepts the connection Decides the version of the protocol. Instantiates a WebSocketProtocol class
  78. 78. how does WebSocketProtocol work? accept_connection Sends the required initial message Reads the next message (asynchronously) _write_response Writes a response on the socket Reads the next message (asynchronously)
  79. 79. ta_websocket.py show me the source!
  80. 80. iostream
  81. 81. what does IOStream do? Communicates with the socket Asynchronous Uses IOLoop Callbacks
  82. 82. how does IOStream work? _add_io_state “Notify me when I can READ or WRITE, or when ERROR” schedules Callback for an event (READ, WRITE, ...) _handle_events Can I read? Call _handle_read Can I write? Call _handle_write Handles errors
  83. 83. how does IOStream work? _handle_read Store data in read buffer Call the read callback (_read_from_buffer) _handle_write Write data from the buffer into the socket (handling funny circumstances) Call the write callback (if it exists)
  84. 84. how does IOStream work? connect socket.connect _add_io_state(WRITE) read Set callback Data in read buffer? Return it Read buffer empty? _add_io_state(READ) write Add data to write buffer _add_io_state(WRITE)
  85. 85. how do I use IOStream directly? read_until_regex read_until read_bytes read_until_close All take a callback
  86. 86. how do I use streaming callbacks? read_bytes read_until_close Param: streaming_callback Data is sent to callback as it arrives
  87. 87. 0a_async_callback.py show me the source!
  88. 88. database
  89. 89. how do I talk to a database? database.connection query(“SQL”) Returns an iterable Very simple
  90. 90. 07_db.py show me the source!
  91. 91. Not async!
  92. 92. asyncmongo
  93. 93. what is asyncmongo? Asynchronous MongoDB client Uses Tornado's IOLoop
  94. 94. how do I use asyncmongo? asyncmongo.Client db.find takes a callback parameter
  95. 95. 08_mongo.py show me the source!
  96. 96. how does asyncmongo work? Implements a Connection Many: ConnectionPool Sends data via Tornado's IOStream Commands send via Cursor Cursor.send_message Uses Connection to communicate asynchronously
  97. 97. can we write our own asyncmysql? Difficult C driver has blocking calls mysql_query mysql_store_result mysql_use_result Alternative Use Twisted's txMySQL with tornado.platform.twisted Slower
  98. 98. Q A&
  99. 99. GRAZIE! source: uncertain (sorry!) dev@oscarvilaplana.cat

×