Live data from Hacker News

Await Is Not a Context Switch: Understanding Python's Coroutines vs. Tasks

mergify.com

71–79 of 79 posts

Re: Await Is Not a Context Switch: Understanding Python's Coroutines vs. Tasks

#71
I'm confused, of course if you await immediately it's not going to have a chance to do anything else _before_ returning.

If you do the following it works as expected

    async def child():
        print("child start")
        await asyncio.sleep(0)
        print("child end")
    
    async def parent():
        print("parent before")
        task = child()
        print("parent after")
        await task
The real difference is that the coroutine is not going to do _anything_ until it is awaited, but I don't think the asyncio task is really different in a meaningful way. It's just a wrapper with an actual task manager so you can run things 'concurrently'.

Python does have two different coroutines, but they're generators and async functions. You can go from one to the other,

Re: Await Is Not a Context Switch: Understanding Python's Coroutines vs. Tasks

#72

My New Year’s Resolution will be to give up complaining about this on hn, but for now: I find ChatGPT’s style and tone condescending and bland to the point of obfuscating whatever was unique, thoughtful and insightful in the original prompt. Trying to reverse-engineer the “Not this: That!” phrasing, artificial narrative drama & bizarre use of emphasis to recapture that insight and thought is not something I’m at all…

It is optimized for marketing speak. It's very appealing to people who 1. don't know this is slop 2. don't read for in-depth knowledge but for entertainment. Similarly coding is optimized for tutorial-sized code - ignoring exceptions, leaving "IN PRODUCTION DO XYZ" comments, etc...

That’s what I mean by condescending. I don’t need to be sold “one neat trick to show off in code reviews”. I actually wanted to learn something about async Python!

Re: Await Is Not a Context Switch: Understanding Python's Coroutines vs. Tasks

#73
post #14
post #4

Sorry if I've got this wrong, but wouldn't the first example behave the same way in Javascript as well? The function parent() "awaits" the completion of child(), so it wouldn't be possible to interleave the print statements. The example from this StackOverflow question might be a better demonstration: https://stackoverflow.com/q/63455683

You haven't got it wrong, the first example in the article behaves the same in JS, see https://jsfiddle.net/L5w2q1p7/ .

I think in JS it's easier to see because of the correspondence between Promises and async/await.

So in your example the behavior is much more obvious if you sort of desugar it as

  async function parent() {
      print("parent before");
      const p = child();
      await p
      print("parent after");
  }

Re: Await Is Not a Context Switch: Understanding Python's Coroutines vs. Tasks

#74
post #30

Earlier quoted context omitted.

Are you saying this was generated by ChatGPT? It didn't seem that way to me at all... what gave that away to you?

Are you being sarcastic? If not it’s obvious once you’ve seen it often enough.

No, I was serious. Perhaps I haven't seen it enough to be able to tell.

Re: Await Is Not a Context Switch: Understanding Python's Coroutines vs. Tasks

#75

Earlier quoted context omitted.

Are you saying this was generated by ChatGPT? It didn't seem that way to me at all... what gave that away to you?

> Putting it all together: a mental model that actually works dead giveaway

I agree that part sounds very ChatGPT-ish (and I didn't notice it before), but just one line generated by AI doesn't indicate to me the text was mostly generated by AI... does it to you? I could totally see the original text being improved by AI here and there in small pieces.

Re: Await Is Not a Context Switch: Understanding Python's Coroutines vs. Tasks

#76
post #36

Earlier quoted context omitted.

Are you saying this was generated by ChatGPT? It didn't seem that way to me at all... what gave that away to you?

Things like "The misconception", "The key truth", "Why ... choose ...", "Putting it all together", "Notice what didn't happen". But not just that but the general wording of this post. From the words of ChatGPT itself: > The post follows a “classic” structure: introduce a common misconception → explain what’s wrong → show concrete examples → give a clear takeaway / conclusion. The paragraphs are well balanced, each ch…

Interesting. I'm probably just lacking in experience with this. I see it better now that you mention it, though it's not clear to me how significant of a component AI would've been...

Re: Await Is Not a Context Switch: Understanding Python's Coroutines vs. Tasks

#77

My New Year’s Resolution will be to give up complaining about this on hn, but for now: I find ChatGPT’s style and tone condescending and bland to the point of obfuscating whatever was unique, thoughtful and insightful in the original prompt. Trying to reverse-engineer the “Not this: That!” phrasing, artificial narrative drama & bizarre use of emphasis to recapture that insight and thought is not something I’m at all…

Are you saying this was generated by ChatGPT? It didn't seem that way to me at all... what gave that away to you?

I think that completely useless example, wrong assumptions that other languages will give different output for it and only bragging about how Python’s model is great, because it gives you the differentiation you can’t understand.

I hope we will get some filtering on HN, so we don’t waste time on such slops

Re: Await Is Not a Context Switch: Understanding Python's Coroutines vs. Tasks

#78

I may be misunderstanding it, but the examples shown don't seem to be illustrative? It seems reasonably obvious that the prints will happen in this order in any language, because in example 1 we are explicitly (a)waiting for the child to finish, and in example 2 both of the parent prints are above the await. So I don't feel like either of these makes the point the author is trying to get across?

I agree. If you strictly follow the syntax of "Example 1" in JavaScript (calling and awaiting on the same line), the observable output is identical to Python. I suppose the author meant to say that if you first called your async function and then later did `await` you would have different behavior.

In every language having any kind of asynchronous features you should get exactly same result. Other comments already mentioned how the example should look and how it differs.

In short: having other coroutine working and awaiting e.g. on sleep() you can get anything between „parent before” and „child start”. In Python is impossible, because child is not run as new task.

Re: Await Is Not a Context Switch: Understanding Python's Coroutines vs. Tasks

#79
post #69

How many people just avoid working with await in Python at all costs (e.g., favoring something like multiprocessing instead?). It’s always felt less comfortable in Python than in other languages

Favor gevent instead.

Maybe in a few years just plain threads when ecosystem is ok for gil less.

Post reply on HN