What's The Tornado Ioloop, And Tornado's Workflow?
Solution 1:
I'll see if I can answer your questions in order:
Here
_impl
is whichever socket polling mechanism is available,epoll
on Linux,select
on Windows. Soself._impl.register(fd, events | self.ERROR)
passes the "wait for some event" request to the underlying operating system, also specifically including error events.When run, the
HTTPServer
will register sockets to accept connections on, usingIOLoop.add_handler()
. When connections are accepted, they will generate more communication sockets, which will likely also add event handlers via anIOStream
, which may also calladd_handler()
. So new handlers will be added both at the beginning, and as connections are recieved.Yes, every new socket connection will have a unique file descriptor. The original socket the
HTTPServer
is listening on should keep its file descriptor though. File descriptors are provided by the operating system.The
IOLoop
handles events to do with the sockets, for example whether they have data available to be read, whether they may be written to, and whether an error has occured. By using operating system services such asepoll
orselect
, it can do this very efficiently.An
IOStream
handles streaming data over a single connection, and uses theIOLoop
to do this asynchronously. For example anIOStream
can read as much data as is available, then useIOLoop.add_handler()
to wait until more data is available.On
listen()
, theHTTPServer
creates a socket which it uses to listen for connections using theIOLoop
. When a connection is obtained, it usessocket.accept()
to create a new socket which is then used for communicating with the client using a newHTTPConnection
.The
HTTPConnection
uses anIOStream
to transfer data to or from the client. ThisIOStream
uses theIOLoop
to do this in an asynchronous and non-blocking way. ManyIOStream
andHTTPConnection
objects can be active at once, all using the sameIOLoop
.
I hope this answers some of your questions. I don't know of a good structural chart, but the overall idea should be fairly similar for other webservers too, so there might be some good info around. That in-depth article you linked to did look pretty useful, so if you understand enough I'd recommend giving it another go :).
Post a Comment for "What's The Tornado Ioloop, And Tornado's Workflow?"