←back to thread

320 points willm | 1 comments | | HN request time: 0.202s | source
Show context
xg15 ◴[] No.45107259[source]
I learned about the concept of async/await from JS and back then was really amazed by the elegance of it.

By now, the downsides are well-known, but I think Python's implementation did a few things that made it particularly unpleasant to use.

There is the usual "colored functions" problem. Python has that too, but on steroids: There are sync and async functions, but then some of the sync functions can only be called from an async function, because they expect an event loop to be present, while others must not be called from an async function because they block the thread or take a lot of CPU to run or just refuse to run if an event loop is detected. That makes at least four colors.

The API has the same complexity: In JS, there are 3 primitives that you interact with in code: Sync functions, async functions and promises. (Understanding the event loop is needed to reason about the program, but it's never visible in the code).

Whereas Python has: Generators, Coroutines, Awaitables, Futures, Tasks, Event Loops, AsyncIterators and probably a few more.

All that for not much benefit in everyday situations. One of the biggest advantages of async/await was "fearless concurrency": The guarantee that your variables can only change at well-defined await points, and can only change "atomically". However, python can't actually give the first guarantee, because threaded code may run in parallel to your async code. The second guarantee already comes for free in all Python code, thanks to the GIL - you don't need async for that.

replies(6): >>45107307 #>>45107536 #>>45108908 #>>45109368 #>>45110090 #>>45112261 #
Retr0id ◴[] No.45107307[source]
> some of the sync functions can only be called from an async function, because they expect an event loop to be present

I recognise that this situation is possible, but I don't think I've ever seen it happen. Can you give an example?

replies(1): >>45108032 #
1. xg15 ◴[] No.45108032[source]
Everything that directly interacts with an event loop object and calls methods such as loop.call_soon() [1].

This is used by most of asyncio's synchronization primitives, e.g. async.Queue.

A consequence is that you cannot use asyncio Queues to pass messages or work items between async functions and worker threads. (And of course you can't use regular blocking queues either, because they would block).

The only solution is to build your own ad-hoc system using loop.call_soon_threadsafe() or use third-party libs like Janus[2].

[1] https://github.com/python/cpython/blob/e4e2390a64593b33d6556...

[2] https://github.com/aio-libs/janus