PyCon lightning talk on my Toro module for Tornado

  • 4,249 views
Uploaded 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. …

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,249
On Slideshare
0
From Embeds
0
Number of Embeds
12

Actions

Shares
Downloads
8
Comments
0
Likes
4

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. ToroSynchronization Primitives for Tornado Coroutines
  • 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. 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. Great!So where are:• Lock• Semaphore• Condition• Event• Queue
  • 5. QueuesA producer-consumer example
  • 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. $ 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. toro.rtfd.org A. Jesse Jiryu Davis 10gen @jessejiryudavis