Professional Documents
Culture Documents
Gavin M. Roy
About myYearbook
According to comScore, myYearbook is one of the 25 mosttrafficked sites in the United States as measured by page views, by minutes, and by minutes per visitor per month.
http://www.myyearbook.com/careers
PyCon 2012 Follow me on Twitter: @Crad
Tornado @ myYearbook
At myYearbook, we used Tornado in highrequest-velocity applications.
PyCon 2012
Callback-Passing Style
PyCon 2012
PyCon 2012
PyCon 2012
tornado.IOLoop
Core of Tornados network stack Fast and easy to use Single instance per process Cross-platform Easy to read source: http://goo.gl/VFeAF Used for client libraries & server
applications
PyCon 2012
Works on windows but not supported.
tornado.IOStream
Convenient utility class for dealing with the
IOLoop
Does most of the work for you This too is for use by clients and server
applications
PyCon 2012 Follow me on Twitter: @Crad
read_until_close
(callback, streaming_callback=None)
PyCon 2012
tornado.netutil.TCPServer
PyCon 2012
Source: http://goo.gl/eB7z4
Follow me on Twitter: @Crad
Slight Magic
PyCon 2012
Diving Deeper
PyCon 2012
tornado.IOLoop
IOLoop.instance() ioloop.add_handler
ioloop.update_handler
ioloop.remove_handler(fd)
PyCon 2012 Follow me on Twitter: @Crad
Events?
Example
import socket from tornado import ioloop def on_events(fd, events, error=None): if events & ioloop.IOLoop.READ: print 'Socket read: %r' % fd.recv(1024) if events & ioloop.IOLoop.ERROR: print 'Error received: %r' % error if events & ioloop.IOLoop.WRITE: pass _ioloop = ioloop.IOLoop.instance() fd = socket.socket() # Other stuff needs to be done here events_desired = ioloop.IOLoop.READ | ioloop.IOLoop.ERROR _ioloop.add_handler(fd, on_events, events_desired) _ioloop.start()
PyCon 2012 Follow me on Twitter: @Crad
Timers
add_timeout
(timeout)
(deadline, callback)
remove_timeout
PyCon 2012
add_callback(callback)
PyCon 2012 Follow me on Twitter: @Crad
Pythonic Simplication
PyCon 2012
tornado.gen
for coding not in Callback Passing Style:
@gen.engine def foo(): http_client = AsyncHTTPClient() response1, response2 = yield [gen.Task(http_client.fetch, url1), gen.Task(http_client.fetch, url2)]
PyCon 2012
tornado.platform.twisted
PyCon 2012 Follow me on Twitter: @Crad
Last Thoughts
Single threaded, use processes Watch out for resource contention Benchmark your application Fork work to remote workers multiprocessing.reduction
PyCon 2012 Follow me on Twitter: @Crad
multiprocessing.reduction:
# Main process from multiprocessing.reduction import reduce_handle h = reduce_handle(client_socket.fileno()) pipe_to_worker.send(h) #instance of multiprocessing.Pipe # Worker process from multiprocessing.reduction import rebuild_handle h = pipe.recv() fd = rebuild_handle(h) client_socket = socket.fromfd(fd, socket.AF_INET, socket.SOCK_STREAM) client_socket.send("hello from the worker process\r\n")
Questions?
PyCon 2012 Follow me on Twitter: @Crad