SlideShare a Scribd company logo
1 of 56
Download to read offline
Tornado Web Server Internals
              Praveen Gollakota
                 @pgollakota
          http://shutupandship.com


   A stroll into (and out of) the eye of the tornado
Agenda
●   Tornado at a glance
●   Sockets background
●   I/O monitoring - select, poll, epoll
●   Tornado server setup loop
●   Tornado request - response loop
●   Tornado vs. Apache
Tornado
Tornado
● Tornado - a scalable, non-blocking web server.
● Also a minimal Web Application Framework.
Written in Python. Open source - Apache V2.0 license.
● Originally built by FriendFeed (acquired by Facebook).

   "The framework is distinct from most mainstream web server
   frameworks (and certainly most Python frameworks) because it is non-
   blocking and reasonably fast. Because it is non-blocking and uses epoll
   or kqueue, it can handle thousands of simultaneous standing
   connections, which means it is ideal for real-time web services." -
   Tornado Web Server Home page blurb.
Tornado modules at a glance
Core web framework                   Integration with other services
 ● tornado.web                        ● tornado.auth
 ● tornado.httpserver                 ● tornado.database
 ● tornado.template                   ● tornado.platform.twisted
 ● tornado.escape                     ● tornado.websocket
 ● tornado.locale                     ●  tornado.wsgi
Asynchronous networking              Utilities
 ● tornado.ioloop — Main event        ● tornado.autoreload
   loop                               ● tornado.gen
 ● tornado.iostream — Convenient      ● tornado.httputil
   wrappers for non-blocking          ● tornado.options
   sockets                            ● tornado.process
 ● tornado.httpclient — Non-          ● tornado.stack_context
   blocking HTTP client               ●  tornado.testing
 ● tornado.netutil — Miscellaneous
   network utilities
Hello World!
from tornado import ioloop
from tornado import web

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        self.write("Hello, world")

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

if __name__ == "__main__":
    srv = httpserver.HTTPServer(app)
    app.listen(8080)
    ioloop.IOLoop.instance().start()
Our Mission
● Analyze "Hello World" application and figure
  out what happens at every step of the way.
  All the way from how the server is setup to
  how the entire request-response cycle works
  under the hood.
● But first a little bit of background about
  sockets and poll.
Sockets
Some background
Sockets
● Network protocols are handled through a
  programming abstraction known as sockets.
  Socket is an object similar to a file that
  allows a program to accept incoming
  connection, make outgoing connections, and
  send and receive data. Before two machines
  can communicate, both must create a socket
  object. The Python implementation just calls
  the system sockets API.
● For more info $ man socket
Sockets - Address, Family
and Type
● Address - Combination of IP address and
  port
● Address family - controls the OSI network
  layer protocol, for example AF_INET for
  IPv4 Internet sockets using IPv4.
● Socket type - controls the transport layer
  protocol, SOCK_STREAM for TCP.
TCP Connection sequence
         Server                                   Client

         socket()                                socket()


          bind()


          listen()


         accept()


    wait for connection   establish connection
                                                 connect()

                               request
          read()                                  write()

    process
                              response
          write()                                 read()
Client Socket Example
#Examples from Socket Programming HOWTO
#create an INET, STREAMing socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#now connect to the web server on port 8080
s.connect(("www.mcmillan-inc.com", 8080))
Server Socket Example
#Examples from Socket Programming HOWTO
#create an INET, STREAMing socket
serversocket = socket.socket(socket.AF_INET,
                             socket.SOCK_STREAM)
#bind the socket to a public host,and a well-known port
serversocket.bind('localhost', 8080))
#become a server socket
serversocket.listen(5)
while True:
    #accept connections from outside
    (clientsocket, address) = serversocket.accept()
    #do something. In this case assume in a different
thread
    ct = client_thread(clientsocket)
    ct.run()
Server Socket explained
“server socket ... doesn’t send any data. It doesn’t receive
any data. It just produces client sockets. Each client socket
is created in response to some other client socket doing a
connect() to the host and port we’re bound to. As soon as
we’ve created that client socket, we go back to listening for
more connections. The two clients are free to chat it up -
they are using some dynamically allocated port which will
be recycled when the conversation ends.”
       - Gordon McMillan in Socket Programming HOWTO
Server Socket Loop
Three options -
● dispatch a thread to handle client socket
● create a new process to handle client socket
● Use non-blocking sockets, and mulitplex
  between our server socket and any active
  client sockets using select.
Sockets - Blocking vs. Non-
blocking
● Blocking sockets - socket API calls will block
  indefinitely until the requested action (send,
  recv, connect or accept) has been
  performed.
● Non-blocking sockets - send, recv,
  connect and accept can return
  immediately without having done anything.
● In Python, you can use socket.
  setblocking(0) to make a socket non-
  blocking.
Handling non-blocking
sockets
“You have (of course) a number of
choices. You can check return
code and error codes and
generally drive yourself crazy. If
you don’t believe me, try it
sometime. Your app will grow
large, buggy and suck CPU. So let’
s skip the brain-dead solutions
and do it right. …


   Use select.”                      Gordon McMillan - Author of
                                     Socket Programming HOWTO &
                                     creator of PyInstaller
References
● Socket Programming HOWTO by Gordon
  McMillan
● Python Module of the Week (PyMOTW) -
  Socket by Doug Hellmann
● Python Essential Reference by David Beazley
select, poll, epoll
 Waiting for I/O efficiently
select
● A system call - allows a program to monitor
  multiple file descriptors, waiting until one or
  more of the file descriptors become "ready"
  for some class of I/O operation
● More info $ man select
● Python’s select() function is a direct
  interface to the underlying operating system
  implementation.
poll
● poll() scales better than select().
● poll() - only requires listing the file
  descriptors of interest, while select()
  builds a bitmap, turns on bits for the fds of
  interest, and then afterward the whole
  bitmap has to be linearly scanned again.
● select() is O(highest file
  descriptor), while poll() is O(number
  of file descriptors).
poll API
● Create a poll object
  p = select.poll()
● Register a fd and the events of interest to be
  notified about
  p.register(fd, events)
● Start monitoring. You will be notified if there
  is an event of interest on any of the
  registered fd's.
  p.poll([timeout])
epoll
● epoll() system call has event notification
  facility.
● So epoll is O(active fd's), poll is O
  (registered fd's)
● So epoll faster than poll (there is debate
  about exactly how much faster, but let's not
  get into that ... because I have no idea).
● Provides exactly same API as poll.
● Tornado tries to use epoll or kqueue and
  falls back to select if it cannot find them.
References
● Python Module of the Week (PyMOTW) -
  select by Doug Hellmann
● The C10K problem by Dan Kegel
● poll, epoll, science, superpoll by Zed Shaw
Tornado
The server loop
Hello World!
from tornado import ioloop
from tornado import web

class MainHandler(web.RequestHandler):
    def get(self):
        self.write("Hello, world")

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

if __name__ == "__main__":
    srv = httpserver.HTTPServer(app)
    app.listen(8080)
    ioloop.IOLoop.instance().start()
app = web.Application(...)
Nothing special here. Just creates an
Application object and adds the handlers to
the handlers attribute.
srv = httpserver.HTTPServer(app)

The constructor of HTTPServer does some
basic setup.




Then calls the constructor of its parent class:
TCPServer
TCPServer.__init__
Basic setup … nothing interesting.
srv.listen(8080)
● First it calls bind_sockets() method
  which creates non-blocking, listening
  server socket (or sockets) bound to the
  given address and port (in this case
  localhost:8080).
● Then creates an instance of the IOLoop
  object
  self.io_loop = IOLoop.instance()
IOLoop.__init__
● New select.epoll object is created.
  self._impl = select.epoll()
● We will register the file descriptors of the
  server sockets with this epoll object to
  monitor for events on the sockets. (will be
  explained shortly).
After IOLoop is instantiated
TCPServer listen() continued
● TCPServer keeps track of the sockets in the _sockets
    dict - {fd: socket}
●   An accept_handler function is created for each socket
    and passed to the IOLoop.add_handlers() method.
●   accept_handler is a thin wrapper around a callback
    function which just accepts the socket (socket.
    accept()) and then runs the callback function.
●   In this case the callback function is the
    _handle_connection method of the TCPServer.
    More on this later.
Adding handlers to IOLoop
● Updates ioloop._handlers, with {fd:
  accept_handler} to keeps track of which
  handler function needs to be called when a
  client tries to establish a connection.
● Registers the fd (file descriptor) and data
  input and error events for the corresponding
  socket with IOLoop._impl (the epoll
  object).
Current status
                 Read and error events on
                 fd's registered with _impl
IOLoop.instance()
● IOLoop.instance()always returns the
  same object, no matter how many times it is
  called.
IOLoop.instance().start()
● start() method starts the IOLoop. The IOLoop is
  the heartbeat and the nerve center of
  everything.
● Continually runs any callback functions, callbacks
  related to any timeouts, and then runs poll() method
  on self._impl the epoll object for any new data
  input events on the socket.
● Note: A connect() request from a client is considered
  as an input event on a server socket.
● There is logic in here to send signals to wake up the I/O
  loop from idle state, ways to run periodic tasks using
  timeouts etc. which we won't get into.
Tornado
The request-response loop
What happens when a client
connects?
● The client socket connect() is captured by the
  poll() method in the IOLoop's start() method.
● The server runs the accept_handler which
  accept()'s the connection, then immediately runs the
  associated callback function.
● Remember that accept_handler is a closure that
  wraps the callback with logic to accept() the
  connection, so accept_handler knows which callback
  function to run.
● The callback function in this case is
  _handle_connection method of TCPServer
TCPServer._handle_connection()

● Creates an IOStream object.
● IOStream is a wrapper around non-
  blocking sockets which provides utilities to
  read from and write to those sockets.
● Then calls HTTPServer.handle_stream
  (...)and passes it the IOStream object
  and the client socket address.
HTTPServer.handle_stream(...)

● handle_stream() method creates a
  HTTPConnection object with our app as a
  request_callback.
● HTTPConnection handles a connection to
  an HTTP client and executes HTTP requests.
  Has methods to parse HTTP headers, bodies,
  execute callback tasks etc.
HTTPConnection.__init__()

● Reads the headers until "rnrn" ...
  delegated to the IOStream object.
  self.stream.read_until(b("rnrn"),
              self._header_callback)
● _header_callback is _on_headers
  method of HTTPConnection. (We'll get to
  that in a moment).
IOStream read
● A bunch of redirections to various _read_* methods.
  Finally once the headers are read and parsed, invokes
  _run_callback method. Invokes the socket.
  recv() methods.
● Call back is not executed right away, but added to the
  IOLoop instance to be called in the next cycle of the IO
  loop.
  self.io_loop.add_callback(wrapper)
● wrapper is just a wrapper around the callback with
  some exception handling. Remember, our callback is
  _on_headers method of HTTPConnection object
HTTPConnection._on_headers

● Creates the appropriate HTTPRequest
  object (now that we have parsed the
  headers).
● Then calls the request_callback and
  passes the HTTPRequest. Remember this?
  May be you don't after all this ...
  request_callback is the original app we
  created.
● Whew! Light at the end of the tunnel. Only a
  couple more steps.
app.__call__
● Application is a callable object (has the
  __call__ method. So you can just call an
  application.
● The __call__ method looks at the url in
  the HTTPRequest and invokes the
  _execute method of appropriate
  RequestHandler - the MainHandler in
  our example.
RequestHandler._execute
● Executes the appropriate HTTP method
  getattr(self,self.request.method.lower()
          )(*args, **kwargs)
● In our case get method calls write() and
  writes the "Hello World" string.
● Then calls finish() method which
  prepares response headers and calls
  flush() to write the output to the socket
  and close it.
Writing the output and closing
● RequestHandler.flush() delegates the write() to
  the request, which in turn delegates it to the
  HTTPConnection which in turn delegates it to the
  IOStream.
● IOStream adds this write method to the IOLoop.
  _callbacks list and the write is executed in turn
  during the next iteration of IOLoop.
● Once everything is done, the socket is closed (unless of
  course you specify that it stay open).
Points to note ...
● Note that we did fork a process.
● We did not spawn a thread.
● Everything happens in just one thread and is
  multiplexed using epoll.poll()
● Callback handlers are run one at a time, in
  turn, on a single thread.
● If a callback task (in the RequestHandler)
  is long running, for example a database
  query that takes too long, the other requests
  which are queued behind will suffer.
Other things to consider
● You can make your request handler
  asynchronous, and keep the connection open
  so that other requests do not suffer.
● But you have to close the connection
  yourself.
● See the chat example in the source code.
Apache vs. Tornado
Apache - multiple requests
● How multiple requests are handled depends
  on Multiprocessing mode (MPM).
● Two modes
  ○ prefork
  ○ worker
prefork MPM
● Most commonly used. Is the default mode in
  2.x and only option in 1.3.
● The main Apache process will at startup
  create multiple child processes. When a
  request is received by the parent process, it
  will be processed by whichever of the child
  processes is ready.
worker MPM
● Within each child process there will exist a
  number of worker threads.
● The request may be processed by a worker
  thread within a child process which already
  has other worker threads handling other
  requests at the same time.
Apache vs. Tornado
● Apache has additional memory overhead of
  maintaining the other processes which are
  essentially idle when the request load is low.
  Tornado does not have this overhead.
● Tornado natively allows you to use
  websockets. Experimental support in
  apache with apache-websocket module.
● Scalability - There are arguments for both
  sides. Personally I haven't built anything that
  cannot be scaled by Apache. So no idea if one
  is better than the other.
References
● Processes and Threading in mod_wsgi wiki
Thank you!

More Related Content

What's hot

Async programming and python
Async programming and pythonAsync programming and python
Async programming and pythonChetan Giridhar
 
Ruby HTTP clients comparison
Ruby HTTP clients comparisonRuby HTTP clients comparison
Ruby HTTP clients comparisonHiroshi Nakamura
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with ExamplesGabriele Lana
 
Implementing Comet using PHP
Implementing Comet using PHPImplementing Comet using PHP
Implementing Comet using PHPKing Foo
 
Presentation: Everything you wanted to know about writing async, high-concurr...
Presentation: Everything you wanted to know about writing async, high-concurr...Presentation: Everything you wanted to know about writing async, high-concurr...
Presentation: Everything you wanted to know about writing async, high-concurr...Baruch Sadogursky
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.jsjacekbecela
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Baruch Sadogursky
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.jsorkaplan
 
WebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkWebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkFabio Tiriticco
 
A complete guide to Node.js
A complete guide to Node.jsA complete guide to Node.js
A complete guide to Node.jsPrabin Silwal
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleTom Croucher
 
Groovy Powered Clean Code
Groovy Powered Clean CodeGroovy Powered Clean Code
Groovy Powered Clean Codenoamt
 
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...Innovecs
 
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningPyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningGraham Dumpleton
 

What's hot (20)

Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
 
Python, do you even async?
Python, do you even async?Python, do you even async?
Python, do you even async?
 
How do event loops work in Python?
How do event loops work in Python?How do event loops work in Python?
How do event loops work in Python?
 
Ruby HTTP clients comparison
Ruby HTTP clients comparisonRuby HTTP clients comparison
Ruby HTTP clients comparison
 
Pycon - Python for ethical hackers
Pycon - Python for ethical hackers Pycon - Python for ethical hackers
Pycon - Python for ethical hackers
 
Nodejs Explained with Examples
Nodejs Explained with ExamplesNodejs Explained with Examples
Nodejs Explained with Examples
 
The future of async i/o in Python
The future of async i/o in PythonThe future of async i/o in Python
The future of async i/o in Python
 
Implementing Comet using PHP
Implementing Comet using PHPImplementing Comet using PHP
Implementing Comet using PHP
 
Presentation: Everything you wanted to know about writing async, high-concurr...
Presentation: Everything you wanted to know about writing async, high-concurr...Presentation: Everything you wanted to know about writing async, high-concurr...
Presentation: Everything you wanted to know about writing async, high-concurr...
 
Introduction to node.js
Introduction to node.jsIntroduction to node.js
Introduction to node.js
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java
 
Introduction to asyncio
Introduction to asyncioIntroduction to asyncio
Introduction to asyncio
 
introduction to node.js
introduction to node.jsintroduction to node.js
introduction to node.js
 
About Node.js
About Node.jsAbout Node.js
About Node.js
 
WebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! FrameworkWebSockets wiith Scala and Play! Framework
WebSockets wiith Scala and Play! Framework
 
A complete guide to Node.js
A complete guide to Node.jsA complete guide to Node.js
A complete guide to Node.js
 
A million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scaleA million connections and beyond - Node.js at scale
A million connections and beyond - Node.js at scale
 
Groovy Powered Clean Code
Groovy Powered Clean CodeGroovy Powered Clean Code
Groovy Powered Clean Code
 
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
Reliable Python REST API (by Volodymyr Hotsyk) - Web Back-End Tech Hangout - ...
 
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance TuningPyCon US 2012 - Web Server Bottlenecks and Performance Tuning
PyCon US 2012 - Web Server Bottlenecks and Performance Tuning
 

Viewers also liked

用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用Felinx Lee
 
Real time server
Real time serverReal time server
Real time serverthepian
 
Crystal is a Rubyists friend (quick anecdote)
Crystal is a Rubyists friend (quick anecdote)Crystal is a Rubyists friend (quick anecdote)
Crystal is a Rubyists friend (quick anecdote)Forrest Chang
 
Phl mongo-philly-tornado-2011
Phl mongo-philly-tornado-2011Phl mongo-philly-tornado-2011
Phl mongo-philly-tornado-2011hangxin1940
 
Introduction to Tornado - TienNA
Introduction to Tornado - TienNAIntroduction to Tornado - TienNA
Introduction to Tornado - TienNAFramgia Vietnam
 
Asynchronous programming in Python
Asynchronous programming in PythonAsynchronous programming in Python
Asynchronous programming in PythonAurynn Shaw
 
What is a tornado?
What is a tornado?What is a tornado?
What is a tornado?Austin
 
Contoh Lembar Catatan Fakta Guru
Contoh Lembar Catatan Fakta GuruContoh Lembar Catatan Fakta Guru
Contoh Lembar Catatan Fakta Guruenesha sie
 
Introduction to Apache Cassandra
Introduction to Apache CassandraIntroduction to Apache Cassandra
Introduction to Apache CassandraRobert Stupp
 
Web backends development using Python
Web backends development using PythonWeb backends development using Python
Web backends development using PythonAyun Park
 
An Overview of Apache Cassandra
An Overview of Apache CassandraAn Overview of Apache Cassandra
An Overview of Apache CassandraDataStax
 
Network Traffic Analysis at a financial institution with 788 branches for 350...
Network Traffic Analysis at a financial institution with 788 branches for 350...Network Traffic Analysis at a financial institution with 788 branches for 350...
Network Traffic Analysis at a financial institution with 788 branches for 350...ManageEngine, Zoho Corporation
 
Cassandra Introduction & Features
Cassandra Introduction & FeaturesCassandra Introduction & Features
Cassandra Introduction & FeaturesDataStax Academy
 
Python Coroutines, Present and Future
Python Coroutines, Present and FuturePython Coroutines, Present and Future
Python Coroutines, Present and Futureemptysquare
 

Viewers also liked (18)

用Tornado开发RESTful API运用
用Tornado开发RESTful API运用用Tornado开发RESTful API运用
用Tornado开发RESTful API运用
 
Real time server
Real time serverReal time server
Real time server
 
Sinatra Ruby Framework
Sinatra Ruby FrameworkSinatra Ruby Framework
Sinatra Ruby Framework
 
Crystal is a Rubyists friend (quick anecdote)
Crystal is a Rubyists friend (quick anecdote)Crystal is a Rubyists friend (quick anecdote)
Crystal is a Rubyists friend (quick anecdote)
 
Phl mongo-philly-tornado-2011
Phl mongo-philly-tornado-2011Phl mongo-philly-tornado-2011
Phl mongo-philly-tornado-2011
 
Introduction to Tornado - TienNA
Introduction to Tornado - TienNAIntroduction to Tornado - TienNA
Introduction to Tornado - TienNA
 
Asynchronous programming in Python
Asynchronous programming in PythonAsynchronous programming in Python
Asynchronous programming in Python
 
What is a tornado?
What is a tornado?What is a tornado?
What is a tornado?
 
Tornado
TornadoTornado
Tornado
 
Contoh Lembar Catatan Fakta Guru
Contoh Lembar Catatan Fakta GuruContoh Lembar Catatan Fakta Guru
Contoh Lembar Catatan Fakta Guru
 
Introduction to Apache Cassandra
Introduction to Apache CassandraIntroduction to Apache Cassandra
Introduction to Apache Cassandra
 
Generators: The Final Frontier
Generators: The Final FrontierGenerators: The Final Frontier
Generators: The Final Frontier
 
Web backends development using Python
Web backends development using PythonWeb backends development using Python
Web backends development using Python
 
An Overview of Apache Cassandra
An Overview of Apache CassandraAn Overview of Apache Cassandra
An Overview of Apache Cassandra
 
Network Traffic Analysis at a financial institution with 788 branches for 350...
Network Traffic Analysis at a financial institution with 788 branches for 350...Network Traffic Analysis at a financial institution with 788 branches for 350...
Network Traffic Analysis at a financial institution with 788 branches for 350...
 
What's new in NetFlow Analyzer 12.2
What's new in NetFlow Analyzer 12.2What's new in NetFlow Analyzer 12.2
What's new in NetFlow Analyzer 12.2
 
Cassandra Introduction & Features
Cassandra Introduction & FeaturesCassandra Introduction & Features
Cassandra Introduction & Features
 
Python Coroutines, Present and Future
Python Coroutines, Present and FuturePython Coroutines, Present and Future
Python Coroutines, Present and Future
 

Similar to Tornado Web Server Internals

04 android
04 android04 android
04 androidguru472
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkAarti Parikh
 
Java Networking
Java NetworkingJava Networking
Java NetworkingSunil OS
 
Network Programming Clients
Network Programming ClientsNetwork Programming Clients
Network Programming ClientsAdil Jafri
 
15network Programming Clients
15network Programming Clients15network Programming Clients
15network Programming ClientsAdil Jafri
 
Build reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQBuild reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQRobin Xiao
 
Scaling application with RabbitMQ
Scaling application with RabbitMQScaling application with RabbitMQ
Scaling application with RabbitMQNahidul Kibria
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagarNitish Nagar
 
Hands on with CoAP and Californium
Hands on with CoAP and CaliforniumHands on with CoAP and Californium
Hands on with CoAP and CaliforniumJulien Vermillard
 
Network-Connected Development with ZeroMQ
Network-Connected Development with ZeroMQNetwork-Connected Development with ZeroMQ
Network-Connected Development with ZeroMQICS
 
Asynchronous programming intro
Asynchronous programming introAsynchronous programming intro
Asynchronous programming introcc liu
 
Socket Programming using Java
Socket Programming using JavaSocket Programming using Java
Socket Programming using JavaRahul Hada
 

Similar to Tornado Web Server Internals (20)

Monkey Server
Monkey ServerMonkey Server
Monkey Server
 
Python networking
Python networkingPython networking
Python networking
 
A.java
A.javaA.java
A.java
 
04 android
04 android04 android
04 android
 
Original slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talkOriginal slides from Ryan Dahl's NodeJs intro talk
Original slides from Ryan Dahl's NodeJs intro talk
 
Socket.io (part 1)
Socket.io (part 1)Socket.io (part 1)
Socket.io (part 1)
 
Sockets
SocketsSockets
Sockets
 
Twisted
TwistedTwisted
Twisted
 
Java Networking
Java NetworkingJava Networking
Java Networking
 
Network Programming Clients
Network Programming ClientsNetwork Programming Clients
Network Programming Clients
 
15network Programming Clients
15network Programming Clients15network Programming Clients
15network Programming Clients
 
Build reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQBuild reliable, traceable, distributed systems with ZeroMQ
Build reliable, traceable, distributed systems with ZeroMQ
 
Scaling application with RabbitMQ
Scaling application with RabbitMQScaling application with RabbitMQ
Scaling application with RabbitMQ
 
Socket Programming - nitish nagar
Socket Programming - nitish nagarSocket Programming - nitish nagar
Socket Programming - nitish nagar
 
Hands on with CoAP and Californium
Hands on with CoAP and CaliforniumHands on with CoAP and Californium
Hands on with CoAP and Californium
 
XML-RPC and SOAP (April 2003)
XML-RPC and SOAP (April 2003)XML-RPC and SOAP (April 2003)
XML-RPC and SOAP (April 2003)
 
Network-Connected Development with ZeroMQ
Network-Connected Development with ZeroMQNetwork-Connected Development with ZeroMQ
Network-Connected Development with ZeroMQ
 
Asynchronous programming intro
Asynchronous programming introAsynchronous programming intro
Asynchronous programming intro
 
Nodejs
NodejsNodejs
Nodejs
 
Socket Programming using Java
Socket Programming using JavaSocket Programming using Java
Socket Programming using Java
 

Recently uploaded

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 

Recently uploaded (20)

Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 

Tornado Web Server Internals

  • 1. Tornado Web Server Internals Praveen Gollakota @pgollakota http://shutupandship.com A stroll into (and out of) the eye of the tornado
  • 2. Agenda ● Tornado at a glance ● Sockets background ● I/O monitoring - select, poll, epoll ● Tornado server setup loop ● Tornado request - response loop ● Tornado vs. Apache
  • 4. Tornado ● Tornado - a scalable, non-blocking web server. ● Also a minimal Web Application Framework. Written in Python. Open source - Apache V2.0 license. ● Originally built by FriendFeed (acquired by Facebook). "The framework is distinct from most mainstream web server frameworks (and certainly most Python frameworks) because it is non- blocking and reasonably fast. Because it is non-blocking and uses epoll or kqueue, it can handle thousands of simultaneous standing connections, which means it is ideal for real-time web services." - Tornado Web Server Home page blurb.
  • 5. Tornado modules at a glance Core web framework Integration with other services ● tornado.web ● tornado.auth ● tornado.httpserver ● tornado.database ● tornado.template ● tornado.platform.twisted ● tornado.escape ● tornado.websocket ● tornado.locale ● tornado.wsgi Asynchronous networking Utilities ● tornado.ioloop — Main event ● tornado.autoreload loop ● tornado.gen ● tornado.iostream — Convenient ● tornado.httputil wrappers for non-blocking ● tornado.options sockets ● tornado.process ● tornado.httpclient — Non- ● tornado.stack_context blocking HTTP client ● tornado.testing ● tornado.netutil — Miscellaneous network utilities
  • 6. Hello World! from tornado import ioloop from tornado import web class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world") app = web.Application([(r"/", MainHandler),]) if __name__ == "__main__": srv = httpserver.HTTPServer(app) app.listen(8080) ioloop.IOLoop.instance().start()
  • 7. Our Mission ● Analyze "Hello World" application and figure out what happens at every step of the way. All the way from how the server is setup to how the entire request-response cycle works under the hood. ● But first a little bit of background about sockets and poll.
  • 9. Sockets ● Network protocols are handled through a programming abstraction known as sockets. Socket is an object similar to a file that allows a program to accept incoming connection, make outgoing connections, and send and receive data. Before two machines can communicate, both must create a socket object. The Python implementation just calls the system sockets API. ● For more info $ man socket
  • 10. Sockets - Address, Family and Type ● Address - Combination of IP address and port ● Address family - controls the OSI network layer protocol, for example AF_INET for IPv4 Internet sockets using IPv4. ● Socket type - controls the transport layer protocol, SOCK_STREAM for TCP.
  • 11. TCP Connection sequence Server Client socket() socket() bind() listen() accept() wait for connection establish connection connect() request read() write() process response write() read()
  • 12. Client Socket Example #Examples from Socket Programming HOWTO #create an INET, STREAMing socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #now connect to the web server on port 8080 s.connect(("www.mcmillan-inc.com", 8080))
  • 13. Server Socket Example #Examples from Socket Programming HOWTO #create an INET, STREAMing socket serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #bind the socket to a public host,and a well-known port serversocket.bind('localhost', 8080)) #become a server socket serversocket.listen(5) while True: #accept connections from outside (clientsocket, address) = serversocket.accept() #do something. In this case assume in a different thread ct = client_thread(clientsocket) ct.run()
  • 14. Server Socket explained “server socket ... doesn’t send any data. It doesn’t receive any data. It just produces client sockets. Each client socket is created in response to some other client socket doing a connect() to the host and port we’re bound to. As soon as we’ve created that client socket, we go back to listening for more connections. The two clients are free to chat it up - they are using some dynamically allocated port which will be recycled when the conversation ends.” - Gordon McMillan in Socket Programming HOWTO
  • 15. Server Socket Loop Three options - ● dispatch a thread to handle client socket ● create a new process to handle client socket ● Use non-blocking sockets, and mulitplex between our server socket and any active client sockets using select.
  • 16. Sockets - Blocking vs. Non- blocking ● Blocking sockets - socket API calls will block indefinitely until the requested action (send, recv, connect or accept) has been performed. ● Non-blocking sockets - send, recv, connect and accept can return immediately without having done anything. ● In Python, you can use socket. setblocking(0) to make a socket non- blocking.
  • 17. Handling non-blocking sockets “You have (of course) a number of choices. You can check return code and error codes and generally drive yourself crazy. If you don’t believe me, try it sometime. Your app will grow large, buggy and suck CPU. So let’ s skip the brain-dead solutions and do it right. … Use select.” Gordon McMillan - Author of Socket Programming HOWTO & creator of PyInstaller
  • 18. References ● Socket Programming HOWTO by Gordon McMillan ● Python Module of the Week (PyMOTW) - Socket by Doug Hellmann ● Python Essential Reference by David Beazley
  • 19. select, poll, epoll Waiting for I/O efficiently
  • 20. select ● A system call - allows a program to monitor multiple file descriptors, waiting until one or more of the file descriptors become "ready" for some class of I/O operation ● More info $ man select ● Python’s select() function is a direct interface to the underlying operating system implementation.
  • 21. poll ● poll() scales better than select(). ● poll() - only requires listing the file descriptors of interest, while select() builds a bitmap, turns on bits for the fds of interest, and then afterward the whole bitmap has to be linearly scanned again. ● select() is O(highest file descriptor), while poll() is O(number of file descriptors).
  • 22. poll API ● Create a poll object p = select.poll() ● Register a fd and the events of interest to be notified about p.register(fd, events) ● Start monitoring. You will be notified if there is an event of interest on any of the registered fd's. p.poll([timeout])
  • 23. epoll ● epoll() system call has event notification facility. ● So epoll is O(active fd's), poll is O (registered fd's) ● So epoll faster than poll (there is debate about exactly how much faster, but let's not get into that ... because I have no idea). ● Provides exactly same API as poll. ● Tornado tries to use epoll or kqueue and falls back to select if it cannot find them.
  • 24. References ● Python Module of the Week (PyMOTW) - select by Doug Hellmann ● The C10K problem by Dan Kegel ● poll, epoll, science, superpoll by Zed Shaw
  • 26. Hello World! from tornado import ioloop from tornado import web class MainHandler(web.RequestHandler): def get(self): self.write("Hello, world") app = web.Application([(r"/", MainHandler),]) if __name__ == "__main__": srv = httpserver.HTTPServer(app) app.listen(8080) ioloop.IOLoop.instance().start()
  • 27. app = web.Application(...) Nothing special here. Just creates an Application object and adds the handlers to the handlers attribute.
  • 28. srv = httpserver.HTTPServer(app) The constructor of HTTPServer does some basic setup. Then calls the constructor of its parent class: TCPServer
  • 29. TCPServer.__init__ Basic setup … nothing interesting.
  • 30. srv.listen(8080) ● First it calls bind_sockets() method which creates non-blocking, listening server socket (or sockets) bound to the given address and port (in this case localhost:8080). ● Then creates an instance of the IOLoop object self.io_loop = IOLoop.instance()
  • 31. IOLoop.__init__ ● New select.epoll object is created. self._impl = select.epoll() ● We will register the file descriptors of the server sockets with this epoll object to monitor for events on the sockets. (will be explained shortly).
  • 32. After IOLoop is instantiated
  • 33. TCPServer listen() continued ● TCPServer keeps track of the sockets in the _sockets dict - {fd: socket} ● An accept_handler function is created for each socket and passed to the IOLoop.add_handlers() method. ● accept_handler is a thin wrapper around a callback function which just accepts the socket (socket. accept()) and then runs the callback function. ● In this case the callback function is the _handle_connection method of the TCPServer. More on this later.
  • 34. Adding handlers to IOLoop ● Updates ioloop._handlers, with {fd: accept_handler} to keeps track of which handler function needs to be called when a client tries to establish a connection. ● Registers the fd (file descriptor) and data input and error events for the corresponding socket with IOLoop._impl (the epoll object).
  • 35. Current status Read and error events on fd's registered with _impl
  • 36. IOLoop.instance() ● IOLoop.instance()always returns the same object, no matter how many times it is called.
  • 37. IOLoop.instance().start() ● start() method starts the IOLoop. The IOLoop is the heartbeat and the nerve center of everything. ● Continually runs any callback functions, callbacks related to any timeouts, and then runs poll() method on self._impl the epoll object for any new data input events on the socket. ● Note: A connect() request from a client is considered as an input event on a server socket. ● There is logic in here to send signals to wake up the I/O loop from idle state, ways to run periodic tasks using timeouts etc. which we won't get into.
  • 39. What happens when a client connects? ● The client socket connect() is captured by the poll() method in the IOLoop's start() method. ● The server runs the accept_handler which accept()'s the connection, then immediately runs the associated callback function. ● Remember that accept_handler is a closure that wraps the callback with logic to accept() the connection, so accept_handler knows which callback function to run. ● The callback function in this case is _handle_connection method of TCPServer
  • 40. TCPServer._handle_connection() ● Creates an IOStream object. ● IOStream is a wrapper around non- blocking sockets which provides utilities to read from and write to those sockets. ● Then calls HTTPServer.handle_stream (...)and passes it the IOStream object and the client socket address.
  • 41. HTTPServer.handle_stream(...) ● handle_stream() method creates a HTTPConnection object with our app as a request_callback. ● HTTPConnection handles a connection to an HTTP client and executes HTTP requests. Has methods to parse HTTP headers, bodies, execute callback tasks etc.
  • 42. HTTPConnection.__init__() ● Reads the headers until "rnrn" ... delegated to the IOStream object. self.stream.read_until(b("rnrn"), self._header_callback) ● _header_callback is _on_headers method of HTTPConnection. (We'll get to that in a moment).
  • 43. IOStream read ● A bunch of redirections to various _read_* methods. Finally once the headers are read and parsed, invokes _run_callback method. Invokes the socket. recv() methods. ● Call back is not executed right away, but added to the IOLoop instance to be called in the next cycle of the IO loop. self.io_loop.add_callback(wrapper) ● wrapper is just a wrapper around the callback with some exception handling. Remember, our callback is _on_headers method of HTTPConnection object
  • 44. HTTPConnection._on_headers ● Creates the appropriate HTTPRequest object (now that we have parsed the headers). ● Then calls the request_callback and passes the HTTPRequest. Remember this? May be you don't after all this ... request_callback is the original app we created. ● Whew! Light at the end of the tunnel. Only a couple more steps.
  • 45. app.__call__ ● Application is a callable object (has the __call__ method. So you can just call an application. ● The __call__ method looks at the url in the HTTPRequest and invokes the _execute method of appropriate RequestHandler - the MainHandler in our example.
  • 46. RequestHandler._execute ● Executes the appropriate HTTP method getattr(self,self.request.method.lower() )(*args, **kwargs) ● In our case get method calls write() and writes the "Hello World" string. ● Then calls finish() method which prepares response headers and calls flush() to write the output to the socket and close it.
  • 47. Writing the output and closing ● RequestHandler.flush() delegates the write() to the request, which in turn delegates it to the HTTPConnection which in turn delegates it to the IOStream. ● IOStream adds this write method to the IOLoop. _callbacks list and the write is executed in turn during the next iteration of IOLoop. ● Once everything is done, the socket is closed (unless of course you specify that it stay open).
  • 48. Points to note ... ● Note that we did fork a process. ● We did not spawn a thread. ● Everything happens in just one thread and is multiplexed using epoll.poll() ● Callback handlers are run one at a time, in turn, on a single thread. ● If a callback task (in the RequestHandler) is long running, for example a database query that takes too long, the other requests which are queued behind will suffer.
  • 49. Other things to consider ● You can make your request handler asynchronous, and keep the connection open so that other requests do not suffer. ● But you have to close the connection yourself. ● See the chat example in the source code.
  • 51. Apache - multiple requests ● How multiple requests are handled depends on Multiprocessing mode (MPM). ● Two modes ○ prefork ○ worker
  • 52. prefork MPM ● Most commonly used. Is the default mode in 2.x and only option in 1.3. ● The main Apache process will at startup create multiple child processes. When a request is received by the parent process, it will be processed by whichever of the child processes is ready.
  • 53. worker MPM ● Within each child process there will exist a number of worker threads. ● The request may be processed by a worker thread within a child process which already has other worker threads handling other requests at the same time.
  • 54. Apache vs. Tornado ● Apache has additional memory overhead of maintaining the other processes which are essentially idle when the request load is low. Tornado does not have this overhead. ● Tornado natively allows you to use websockets. Experimental support in apache with apache-websocket module. ● Scalability - There are arguments for both sides. Personally I haven't built anything that cannot be scaled by Apache. So no idea if one is better than the other.
  • 55. References ● Processes and Threading in mod_wsgi wiki