The document discusses the future of asynchronous I/O in Python. It introduces PEP 3156 and the new asyncio module in Python 3.4 as a standard library for asynchronous I/O. It describes how asyncio (called Tulip in the presentation) provides primitives like event loops, transports, protocols and coroutines to build asynchronous applications and frameworks in a pluggable, coroutine-friendly way. It provides examples of using Tulip to run asynchronous tasks and build an echo server.
4. help(async_io)
Blocking Is Bad (TM)
Threads → overhead
i/o multiplexors: “Iʼll let you know
when you can write”
Windows: “Iʼll let you know when
Iʼm done writing”
Thursday, October 3, 2013
8. class MyLoop
Almost no reusable code between
frameworks
Multiple equivalent event loop
implementations
Multiple protocol implementations
HTTP, anyone?
Thursday, October 3, 2013
10. import tulip
PEP-3156: Async I/O support rebooted
A set of components / primitives for async i/o
Python >= 3.3
First version to ship with Python 3.4
(Boring) name for stdlib: asyncio
Thursday, October 3, 2013
11. dir(tulip)
Pluggable event loop
Easy to build adapters
Transport / protocol abstractions
Inspired by Twisted
High-level scheduler based on PEP-380 (yield
from), Futures, Tasks
Coroutine friendly Queue, Lock, Event, ...
Thursday, October 3, 2013
13. from tulip import tasks
from tulip import events, tasks
@tasks.coroutine
def sleeper(name, n):
while True:
yield from tasks.sleep(n, loop=loop)
print('Hello from', name)
loop = events.new_event_loop()
t1 = tasks.async(sleeper('Task1', 1), loop=loop)
t2 = tasks.async(sleeper('Task2', 2), loop=loop)
loop.run_forever()
Thursday, October 3, 2013
14. echo echo echo
import os
from tulip import events, protocols
class EchoProtocol(protocols.Protocol):
def connection_made(self, transport):
print('Client connected')
self.transport = transport
def data_received(self, data):
print('Received data:',data)
self.transport.write(data)
def connection_lost(self, exc):
print('Connection closed', exc)
def Factory():
return EchoProtocol()
loop = events.new_event_loop()
f = loop.start_serving(Factory, '127.0.0.1', int(os.getenv('PORT') or 1234))
sockets = loop.run_until_complete(f)
print('Server started')
loop.run_forever()
Thursday, October 3, 2013
15. print(sys.modules)
People already started implementing stuff
with Tulip
Redis client, websockets, Gunicorn
worker, ...
https://code.google.com/p/tulip/wiki/
ThirdParty
Make your next library Tulip compatible!
(bonus points if it has a nice flower name)
Thursday, October 3, 2013
16. import __future__
All current frameworks would
integrate with Tulip, directly or using
an adapter
Reusable protocols
Integration with Twistedʼs Deferred
...
Thursday, October 3, 2013