pycon lightning talk on my toro module for tornado
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.TRANSCRIPT
Toro
Synchronization Primitives for Tornado Coroutines
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")
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")
Great!
So where are:• Lock• Semaphore• Condition• Event• Queue
Queues
A producer-consumer example
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()
$ 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$
A. Jesse Jiryu Davis10gen
@jessejiryudavis
toro.rtfd.org