News for hackers. Stuff that matters.
Re:Python Asyncio: The Complete Guide by acedTrex on Thursday November 10, 2022 @10:49AM (#33548181)
This is an insane take, if you are doing async you already HAVE a need for concurrent programming. Async just makes that simpler to read and write.
Re:Python Asyncio: The Complete Guide by qbasic_forever on Thursday November 10, 2022 @11:41AM (#33549143)
Async python by design is not concurrent, nothing is executing at the same time on different cores/threads. There's one worker loop and one task running on it at a time.
Re:Python Asyncio: The Complete Guide by _bohm on Thursday November 10, 2022 @11:45AM (#33549211)
I think you mean it is not parallel. Cooperative multitasking is absolutely a type of concurrency
Re:Python Asyncio: The Complete Guide by srik on Thursday November 10, 2022 @11:55AM (#33549410)
Concurrency is not Parallelism. You should watch this quintessential talk about the difference - https://www.youtube.com/watch?v=oV9rvDllKEg
Re:Python Asyncio: The Complete Guide by dekhn on Thursday November 10, 2022 @02:07PM (#33551461)
This is Rob's perspective, but it's not universally shared. In my mind, all concurrency is a form of parallelism (parallel tasks, but not parallel threads) while not all parallelism is concurrency. I have frequently solved concurrency problems with thread pools, rather than using non-blocking IO, because the programming paradigm is a lot simpler.
Re:Python Asyncio: The Complete Guide by BerislavLopac on Tuesday November 15, 2022 @03:32PM (#33614614)
You got the the wrong way round: all parallelism is a form of concurrency.

In Python, thread pools and async work mostly the same in practice, except that threads have a bigger overhead. So they can be used to solve the same problems.

Re:Python Asyncio: The Complete Guide by calpaterson on Thursday November 10, 2022 @11:34AM (#33549007)
Unfortunately the majority of people who use asyncio in the real world do not have a need for concurrenct programming. They are drawn to it because they believe it is faster, which, generally, it is not. That isn't to say there aren't reasonable usecases - but most people using asyncio are writing webapps which spend a lot of time in the CPU generating or outputting JSON/GraphQL/HTML.
Re:Python Asyncio: The Complete Guide by nine_k on Thursday November 10, 2022 @11:43AM (#33549177)
I think this may be influenced by Node. With JS and Node, async is cheap and ergonomic, so it's very widely used, even if the latency gains are marginal.

With Python and asyncio, neither assumption holds, but the idea of shaving off some latency persists: if we rewrite everything using asyncio, our DB accesses can be parallelized, yay! This may or may not be a net win in latency; reworking your DB access patterns may gain you more.

Re:Python Asyncio: The Complete Guide by kissgyorgy on Thursday November 10, 2022 @12:06PM (#33549638)
> reworking your DB access patterns may gain you more.

Nope: https://techspot.zzzeek.org/2015/02/15/asynchronous-python-a...

Re:Python Asyncio: The Complete Guide by matsemann on Thursday November 10, 2022 @01:25PM (#33550886)
I think you agree with the quote? Aka that asyncio doesn't give you a speedup, you might as well keep them the traditional way but instead focus on improving what you already have.
Re:Python Asyncio: The Complete Guide by nine_k on Thursday November 10, 2022 @01:27PM (#33550918)
I don't say that asyncio gives you nothing! Go for it with your DB access if you have already gone async in your design.

But often the lower-hanging fruit is doing more joins in the DB, fetching fewer columns, and writing your query more thoughtfully. Async only helps if you can do something while waiting for the DB to complete your query. A common anti-pattern, exacerbated by ORMs, is doing a bunch of small queries while passing bits of data between them in Python; moving that to asyncio won't help much, if any.

Re:Python Asyncio: The Complete Guide by calpaterson on Thursday November 10, 2022 @02:06PM (#33551448)
> But often the lower-hanging fruit is doing more joins in the DB, fetching fewer columns, and writing your query more thoughtfully.

Reluctant to add a "me-too" comment but I think you have it there. For whatever reason people are much keener to investigate different ways of scheduling IO than they are to investigate what the query plan looks like and why.

Re:Python Asyncio: The Complete Guide by _bohm on Thursday November 10, 2022 @02:23PM (#33551689)
It may not be the norm, but there are definitely apps/services out there that require heavy-duty database queries and can have their throughput bottlenecked by synchronous DB IO latency, and would benefit from asyncio on the database side. Also, SQLAlchemy supports asyncio now.
Re:Python Asyncio: The Complete Guide by throwawaymaths on Thursday November 10, 2022 @10:53AM (#33548241)
> Async just makes that simpler to read and write.

Really? I definitely had a huge struggle reading async python, because generators and yields break the concept of "what is a function" and struggled writing async python because of function coloring.

Re:Python Asyncio: The Complete Guide by andrewstuart on Thursday November 10, 2022 @11:38AM (#33549087)
Day to day Python async should be using generators and yields.
Re:Python Asyncio: The Complete Guide by nine_k on Thursday November 10, 2022 @11:46AM (#33549228)
Generators are great for predictable concurrency. Things like asynchronous I/O, with timeouts and stuff, require more support on the language and stdlib level.
Re:Python Asyncio: The Complete Guide by dekhn on Thursday November 10, 2022 @02:12PM (#33551530)
I am really not a fan of generators and yields for the same reason. But over time I've come to see it as ever-so-slightly more elegant syntax sugar for a class that maintains internal state to sequentially return specific values in response to a series of calls to a function.
Re:Python Asyncio: The Complete Guide by throwawaymaths on Thursday November 10, 2022 @02:25PM (#33551712)
The least they could have done is sugared it to something that isn't "def", like "gen" or "defgen"
Re:Python Asyncio: The Complete Guide by jborean93 on Thursday November 10, 2022 @03:18PM (#33552648)
Unless I'm misunderstanding your comment they do, it's `async def` to denote a coroutine.
Re:Python Asyncio: The Complete Guide by dekhn on Thursday November 10, 2022 @04:09PM (#33553406)
They're referring to generator functions. They typically open a resource and then use a for load to read (for example) a line at a time and yield it. once you yield, control returns to the caller (like a return) but subsequent calls to next on the returned expression pick back up at the yield point. So function calls maintain state-behind-the-scenes. It's not just a simple call-and-return model anymore.

To me this violates a core principle: the principle of least surprise. It completely changes how I have to reason about function execution, especially if there are conditionals and different yields.

Re:Python Asyncio: The Complete Guide by nicwolff on Thursday November 10, 2022 @05:11PM (#33554230)
It's the buried `yield` in a loop somewhere, magically changing your function into a generator, that can be confusing – it's on the original coder to have the docstring say `"""Generator yielding records from DB."""` or something.
Re:Python Asyncio: The Complete Guide by kissgyorgy on Thursday November 10, 2022 @11:00AM (#33548369)
I'm pretty sure a lot of people don't understand the tradeoffs and don't really need concurrent code. There are really only a couple of use-cases where it's really handy: for example an API gateway, where you only get and send HTTP requests. Other than that, I don't see how it worth the insane complexity (compared to sync code).

You can write web servers in a sync manner; Flask, Django is way better if you only need an API, choosing FastAPI for a simple REST API is a huge mistake.

Re:Python Asyncio: The Complete Guide by thunky on Thursday November 10, 2022 @12:55PM (#33550438)
> FastAPI for a simple REST API is a huge mistake

FastAPI can be sync or async, although sync will have "a small performance penalty"

https://github.com/tiangolo/fastapi/issues/260#issuecomment-...

Re:Python Asyncio: The Complete Guide by walls on Thursday November 10, 2022 @01:18PM (#33550771)
What kind of APIs are you writing that don’t involve db lookups or calls to other services?
Re:Python Asyncio: The Complete Guide by samwillis on Thursday November 10, 2022 @01:43PM (#33551138)
Asyncio does not make your db calls or io faster, it does make your code more complex.

The only thing async io gives you is scalability of many MANY concurrent io operations. You have to either be pushing seriously large traffic or doing a lot of long running websocket/sse/long polling type requests.

The only other use case is lots of truly concurrent io within one request/response cycle. But again that is unusual, most apis have low single digit db queries that are usually dependent on one another removing any advantage of async.

Re:Python Asyncio: The Complete Guide by fprct on Friday November 11, 2022 @04:03AM (#33558571)
> The only other use case is lots of truly concurrent io within one request/response cycle. But again that is unusual, most apis have low single digit db queries that are usually dependent on one another removing any advantage of async.

Why just "within one cycle"? What about situation where you spend 1s in single db query and are 100ms cpu bound during request handling?

Re:Python Asyncio: The Complete Guide by samwillis on Saturday November 12, 2022 @05:29AM (#33571829)
Transitional multithreaded Python web servers handle that very well and have for decades, you shouldn't need to manually handle yealding in simple cases in your application code.

The point is asyncio is to have find grade control of yealding.

Re:Python Asyncio: The Complete Guide by mixmastamyk on Thursday November 10, 2022 @01:54PM (#33551302)
Choosing fastapi for a fast api is a mistake?
Re:Python Asyncio: The Complete Guide by kissgyorgy on Thursday November 10, 2022 @02:26PM (#33551735)
Choosing unnecessary complexity for a simple task is a mistake. FastAPI is not faster than Django or Flask for a single request, quite the opposite.
Re:Python Asyncio: The Complete Guide by hdjjhhvvhga on Thursday November 10, 2022 @02:34PM (#33551859)
Re:Python Asyncio: The Complete Guide by lucasyvas on Thursday November 10, 2022 @11:40PM (#33557365)
How is Django less complex than FastAPI? Django was built for monolithic web applications - it does fine with REST, but your position is backward. FastAPI is purpose built for smaller APIs