Python, asyncio, and systemd

Chris Angelico rosuav at gmail.com
Tue Jan 17 11:57:51 EST 2017


If I write a web server using asyncio (and the aiohttp package), I can
spin up the server with:

await loop.create_server(app.make_handler(), "0.0.0.0", 8080)

This works fine for a high port, but if I want to bind to port 80, I
need to either start as root and then drop privileges, or get given
the socket by someone else. With systemd, the latter is an option; you
configure a service unit and a socket unit, and when the service gets
started, it's given a bound listening socket as FD 3.

Most of the work is pretty straight-forward, but I ran into one
problem. The event loop's create_server() method calls a private
_start_serving method, which means I can't (or rather, I shouldn't)
just replicate create_server. This works, but it's naughty:

sock = socket.socket(fileno=3)

sock.setblocking(False)
loop._start_serving(app.make_handler(), sock)

What's the official way to say to asyncio "here's a listening socket,
start managing it for me"?

ChrisA



More information about the Python-list mailing list