PyCon lightning talk on my Toro module for Tornado

5,739 views

Published on

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.

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,739
On SlideShare
0
From Embeds
0
Number of Embeds
3,056
Actions
Shares
0
Downloads
16
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

PyCon lightning talk on my Toro module for Tornado

  1. 1. ToroSynchronization Primitives for Tornado Coroutines
  2. 2. 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")
  3. 3. 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")
  4. 4. Great!So where are:• Lock• Semaphore• Condition• Event• Queue
  5. 5. QueuesA producer-consumer example
  6. 6. 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 tt, Got, item q.task_done()producer()consumer()loop = ioloop.IOLoop.instance()# block until all tasks are doneq.join(callback=loop.stop)loop.start()
  7. 7. $ 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$
  8. 8. toro.rtfd.org A. Jesse Jiryu Davis 10gen @jessejiryudavis

×