8

Click here to load reader

PyCon lightning talk on my Toro module for Tornado

Embed Size (px)

DESCRIPTION

With Tornado’s gen module, you can turn Python generators into full-featured coroutines, but coordination among these coroutines is difficult without mutexes, semaphores, and queues. Toro provides to Tornado coroutines a set of locking primitives and queues analogous to those that Gevent provides to Greenlets, or that the standard library provides to threads.

Citation preview

Page 1: PyCon lightning talk on my Toro module for Tornado

Toro

Synchronization Primitives for Tornado Coroutines

Page 2: PyCon lightning talk on my Toro module for Tornado

Vanilla Tornadoclass AsyncHandler(RequestHandler): @asynchronous def get(self): http_client = AsyncHTTPClient() http_client.fetch("http://example.com", callback=self.on_fetch)

def on_fetch(self, response): do_something_with_response(response) self.render("template.html")

Page 3: PyCon lightning talk on my Toro module for Tornado

tornado.genclass GenAsyncHandler(RequestHandler): @asynchronous @gen.engine def get(self): http_client = AsyncHTTPClient() response = yield gen.Task( http_client.fetch, "http://example.com") do_something_with_response(response) self.render("template.html")

Page 4: PyCon lightning talk on my Toro module for Tornado

Great!

So where are:• Lock• Semaphore• Condition• Event• Queue

Page 5: PyCon lightning talk on my Toro module for Tornado

Queues

A producer-consumer example

Page 6: PyCon lightning talk on my Toro module for Tornado

q = toro.JoinableQueue(maxsize=3)

@gen.enginedef producer(): for item in range(10): print 'Sending', item yield gen.Task(q.put, item)

@gen.enginedef consumer(): while True: item = yield gen.Task(q.get) print '\t\t', 'Got', item q.task_done()

producer()consumer()loop = ioloop.IOLoop.instance()# block until all tasks are doneq.join(callback=loop.stop)loop.start()

Page 7: PyCon lightning talk on my Toro module for Tornado

$ python examples/producer_consumer_example.pySending 0Sending 1Sending 2Sending 3! ! Got 0! ! Got 1! ! Got 2! ! Got 3Sending 4! ! Got 4Sending 5! ! Got 5Sending 6! ! Got 6Sending 7! ! Got 7Sending 8! ! Got 8Sending 9! ! Got 9$

Page 8: PyCon lightning talk on my Toro module for Tornado

A. Jesse Jiryu Davis10gen

@jessejiryudavis

toro.rtfd.org