“The future of async i/o in Python”, slides

Today I had the pleasure to talk at the Amsterdam Python Meetup Group about Tulip, PEP-3156 and async i/o in Python. Here are the slides:

Big thank you to the guys at Byte for hosting tonight’s meetup and providing us with nice drinks and pizza 🙂

Already looking forward to the next one!

:wq

Rose: a PEP-3156 compatible event loop based on pyuv

For those who don’t know, PEP 3156 is a proposal for asynchronous I/O in Python, starting with Python 3.3. Until now each framework (Twisted, Tornado, …) has defined it’s own interface for defining protocols and transports. This makes very difficult if not impossible to reuse a protocol implementation across frameworks. PEP 3156 tries to fix that, among other things.

The reference implementation is called Tulip and can be found here. It’s a fast moving target, but it already contains working event loops for Windows and Unix systems. It uses pollers available in the select module for the Unix side, and a C module wrapping Windows IOCP functionality for Windows.

I was really excited to see this come through, so I started playing with it by implementing a pyuv based event loop. I called that it rose. It was a lot easier to implement than expected and it currently passes the entire test suite 🙂

Code can be found on GitHub.

Here is a quick example, the usual echo server, using rose and tulip:

import signal
from rose import EventLoopPolicy
from tulip import events, protocols
events.set_event_loop_policy(EventLoopPolicy())
class EchoProtocol(protocols.Protocol):
def connection_made(self, transport):
# TODO: Transport should probably expose getsockname/getpeername
print("Client connected: {}".format(transport._sock.getpeername()))
self.transport = transport
def data_received(self, data):
self.transport.write(data.upper())
def eof_received(self):
self.transport.close()
def connection_lost(self, exc):
print("Client closed connection")
reactor = events.get_event_loop()
f = reactor.start_serving(EchoProtocol, '127.0.0.1', 1234)
server_socket = reactor.run_until_complete(f)
print("Serving on {}".format(server_socket.getsockname()))
reactor.add_signal_handler(signal.SIGINT, reactor.stop)
reactor.run()
view raw echo-server.py hosted with ❤ by GitHub

Come and join the discussion in the python-ideas mailing list!

:wq