Hacker News new | ask | show | jobs
by dayjah 1870 days ago
> and async in Python is a mess

I’ve heard this a lot of late, but it’s not clear to me why. Would you be willing to expand on your assertion?

Personally, I’ve written a fair chunk of typescript and asyncio based Python over the year for a project and in general I think Python has done an excellent job of their implementation.

Some things are annoying; for example I’ve a consistent case, which I haven’t pared down to better understand, where by exceptions in a coro are never visible. Not to be confused with the classic case of being notified of the exception when your process shuts down.

OTOH some things are really amazing, for example PYTHONASYNCIODEBUG=1. More so the ability to wrap non async, typically blocking, code in an Executor and it “magically” (via thread pools) work in the event loop has also been a boon for the type of work I’m engaged in.

Waaaaay back, I was really hurt by twisted and to a lesser extent tornado. But the async stuff shipped in 3.9x seems completely comparable to JavaScript imho.

1 comments

The main problem is that many Python libraries come in only sync flavor and using them together with asyncio is a pain.

NodeJS libraries are almost all standardized to async now. Hell, even Tensorflow.js can give you Promises of computation results.

Seems pretty painless to me:

    $ python3 -m asyncio
    >>> from requests import get
    >>> url = 'https://example.com'
    >>> response = await asyncio.to_thread(get, url)
A lot of Python libraries now offer async APIs, have async support on their short term roadmaps, or other async projects have replaced them.
oh neat! I did not know about asyncio.to_thread; I've been using the longer form thread executor pattern.
I hadn't seen that either - looks like it's new in Python 3.9: https://docs.python.org/3/library/asyncio-task.html#asyncio....
And if you aren't using 3.9, the anyio package has a run_async() function in its to_thread module[1] that works the same way as asyncio's to_thread() coroutine function, and the anyio solution is compatible with both asyncio and trio.

[1] https://anyio.readthedocs.io/en/stable/threads.html