Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Literally just using async def for functions that do IO and write await.

If only it were that simple.

> You will get far more trouble using third-party concurrency libraries than trying to use the Python library which is actually very elegant

Again, you overlook the fact that asyncio requires you to explicitly launch a concurrent context. Asyncio is fine, but it is not "elegant" because of this.



> asyncio requires you to explicitly launch a concurrent context

`asyncio.run()` [0] was added in 3.7, and as far as I know is the current recommended way to do this.

as shown in the asyncio "hello world" example [1], you can write your main() function to be async, and then the entire process runs asynchronously from the beginning.

and as shown on that page, you can also use `python -m asyncio` to get a REPL with a running event loop where using `await` will work as you'd expect it to.

one of my biggest quibbles with a lot of asyncio demo code snippets is the use of `loop.run_until_complete()` [2] and similar lower-level primitives. in most asyncio applications you simply want to start the loop as early as possible with `asyncio.run(main())`, let it run for the duration, and have the entire process exist in async-land.

0: https://docs.python.org/3/library/asyncio-runner.html#asynci...

1: https://docs.python.org/3/library/asyncio.html

2: https://stackoverflow.com/questions/40143289/why-do-most-asy...


Yes, these are some ways to get that context. But it can be difficult to do correctly for brownfield projects or libraries, especially if you need to support both sync and async. Not elegant!


> especially if you need to support both sync and async

I don't think there's any possible way to do that elegantly.

maybe gevent-style monkeypatching makes it look elegant, but there's a whole pile of inelegant stuff happening under the covers to present that illusion.

and I would say that almost by definition, if you're monkeypatching the stdlib, you are banished from the realm of elegance.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: