Tulip – Python async I/O with coroutines

Notes From Guido van Rossum talk at LinkedIn, Mountain View, 2014-01-23
Only available in Python 3.4 and later, compatible with Python 3.3
There are multiple ways of doing asynchronous I/O (files & network)
  • OS threads
  • UNIX “selects”
  • Windows “ready callback”
Use transports/protocols as the higher level abstraction
Underneath it’s still an event loop
  1. Coroutines / Futures /  Tasks
  2. Event loop & loop policy
  3. Transports & Protocols


import asyncio

def generator_func( ... ):
    yield from some_method( ... )
Tulip uses Futures and Tasks:
representing an eventual result
a Future wrapping a coroutine
Code Example
def fetch(host, port):
    r, w = yield from open(host, port)
    w.write(r'GET /index.html \r\n\r\n')
    while (yield from r.readline())
    body = yield from r.read()
    return body
 Yes, but what does that mean?
  • Imagine yield from isn’t there
  • each function is sequential code
  • the ‘yield from‘ are used only on blocking I/O calls
import concurrent.futures.Future
example usage for a Future:
f = Future()
res = f.result()
f.done()  # are we there yet?
 We can set a callback (or several) on a Future:
yield from f
 once it’s done() it will call cb
 Typically a Future is used with a blocking method call:
res = yield from some_function( ... )
which returns a future and blocks
A coroutine wrapped in a Future (a subclass of Future)
r = yield from Task(some_coroutine( ... ))
 Use a Task when you want progress even while you’re waiting for something else.
Further Reading
  1. PEP-3156: http://www.python.org/dev/peps/pep-3156/
  2. Code: http://code.google.com/p/tulip/
  3. Python Futures: http://docs.python.org/3.4/library/concurrent.futures.html
  4. Link to Streaming Video (probably a recorded version will be available there too?)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: