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

How do event loops work in Python?

I had the pleasure to give a presentation at the first ever Python Devroom at FOSDEM. I talked about how event loops work internally and how pyuv can help by abstracting a lot of the problems with a pretty simple to use API. I also introduced rose, a pyuv based PEP-3156 event loop implementation, but I’ll write a followup post on that 🙂

Thanks a lot to everyone who attended the talk, and for those who couldn’t here are the slides!

:wq