Toro



Synchronization Primitives for
      Tornado Coroutines
Vanilla Tornado
class 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")
tornado.gen
class 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")
Great!
So where are:
• Lock
• Semaphore
• Condition
• Event
• Queue
Queues


A producer-consumer example
q = toro.JoinableQueue(maxsize=3)

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

@gen.engine
def 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 done
q.join(callback=loop.stop)
loop.start()
$ python examples/producer_consumer_example.py
Sending 0
Sending 1
Sending 2
Sending 3
! ! Got 0
! ! Got 1
! ! Got 2
! ! Got 3
Sending 4
! ! Got 4
Sending 5
! ! Got 5
Sending 6
! ! Got 6
Sending 7
! ! Got 7
Sending 8
! ! Got 8
Sending 9
! ! Got 9
$
toro.rtfd.org



   A. Jesse Jiryu Davis
           10gen
     @jessejiryudavis

PyCon lightning talk on my Toro module for Tornado