The state of asyncio in 2019

Chris Angelico rosuav at gmail.com
Mon May 20 00:40:15 EDT 2019


On Mon, May 20, 2019 at 11:56 AM Becaree
<pierrelouis.chevallier60 at gmail.com> wrote:
>
> So i started to use asyncio some times ago. I see a lots of threads/forums which date back from 2015 or older. My question is what the state of asyncio in 2019? I guess, the major constrain of having async/await is quiet a drawback. I do love asyncio but reinventing the wheel using it is somehow complicated for a community. I mean i found disappointed that major scheduler don't use it as airflow/luigi/dask. Also pulsar is kind of unmaintained. So now i wonder, should i stick with asyncio or some async/await-freed coroutines as use tornado or gevent is the way to go?
>

It depends a bit on what you're trying to do, but if you want a system
that doesn't use async/await, you may want to first look at the
built-in "threading" module. It doesn't scale to infinity the way
asyncio can, but for workloads of up to a few thousand threads, it
should be fine. In the context of an internet server, for instance,
that would mean you can handle a few thousand concurrent requests by
simply having that many threads, each one handling one request at a
time.

Otherwise, there's really not a lot that's fundamentally *bad* about
using the await keyword everywhere that you need to block. Just keep
an eye on your libraries' documentation, and if it says something's a
coroutine, slap an await in front of it. I've used asyncio in several
projects, and it doesn't feel all that onerous. For instance, this
function:

https://github.com/Rosuav/csgo_automute/blob/master/server.py#L47

responds to an HTTP request by saying "okay, wait till you have the
whole body, and JSON-decode it", then does some synchronous work, and
then says "ooh that was interesting, let's broadcast that to all
clients". Pretty simple. The websocket handler above it is similar;
although at the moment, it's very stubby, so it's probably not a great
example. (All it does is listen for messages and then print them on
the console. Mucho excitement.) That's using aiohttp. For vanilla
socket services, you can do everything using the standard library
directly - either start a server (to bind and listen), or connect a
socket, and then await your reads and your writes.

The past four years have definitely seen notable improvements to
asyncio in Python.

ChrisA


More information about the Python-list mailing list