3 ms·
The primary thing that tripped me up about async/await, specifically only in Python, is that the called function does not begin running until you await it. Befo
by nilamo 2y ago
The primary thing that tripped me up about async/await, specifically only in Python, is that the called function does not begin running until you await it. Before that moment, it's just an unstarted generator.
To make background jobs, I've used the class-based version to start a thread, then the magic method that's called on await simply joins the thread. Which is a lot of boilerplate to get a little closer to how async works in (at least) js and c#.
- LegionMammal978 2y agoRust's version of async/await is the same in that respect, where futures don't do anything until you poll them (e.g., by awaiting them): if you want something to just start right away, you have to call out to the executor you're using, and get it to spawn a new task for it. Though to be fair, people complain about this in Rust as well. I can't comment much on it myself, since I haven't had any need for concurrent workloads that Rayon (a basic thread-pool library with work stealing) can't handle.
- pests 2y agoThat is a common split in language design decisions. I think the argument for the python-style where you have to drive it to begin is more useful as you can always just start it immediately but also let's you delay computation or pass it around similar to a Haskell thunk.
- kristjansson 2y agoThere is also https://docs.python.org/3/library/asyncio-task.html#eager-task-factory https://docs.python.org/3/library/asyncio-task.html#eager-ta... if you want your task to start on creation.