multipart-message nevyn bengtsson's blog

featured articles 🦄, about, archive, tags

Thinking alike: await/async in Python 3

Thinking alike: await/async in Python 3

Guido van Rossum’s 2013 PyCon keynote presents Python 3.4’s major new feature: coroutines as a language feature for working with asynchronous operations. I wonder where I’ve seen that before? :)

Async/await is such an amazingly simple way to express asynchrony, and I’m very happy to see more languages adopt the thinking. Also, python is *finally finally finally* standardizing on a runloop API. That has been a crazy big gap in the standard libraries and frankly I don’t understand how people have been able to write Python apps without it…

Example python code with the new API below. “yield from” is identical to “await” in C#.

def getresp():
    s = socket()
    yield from loop.sock_connect(s, host, port)
    yield from loop.sock_sendall(s, b'xyzzy')
    data = yield from loop.sock_recv(s, 100)

Tagged