3 ms·
Trio is a much simpler design that's just as powerful as `asyncio`. It has exactly one way to wait for a task: await it. https://trio.readthedocs.io/ https://t
by j88439h84 6y ago
Trio is a much simpler design that's just as powerful as `asyncio`. It has exactly one way to wait for a task: await it.
https://trio.readthedocs.io/ https://trio.readthedocs.io/
For a group of tasks, use a nursery.
Edit: correction
- nerdwaller 6y agoTo save others the search regarding a nursery: https://trio.readthedocs.io/en/stable/tutorial.html#okay-let-s-see-something-cool-already https://trio.readthedocs.io/en/stable/tutorial.html#okay-let... I don’t see much more useful here than understanding the asyncio primitives and available synchronization abstractions, just a different API mostly overlapping the same need To be clear, trio also has more than one way to wait for a task - any await is the same (e.g. `await trio.sleep(N)` (e.g. `await asyncio.sleep(N)`). I think maybe you’re getting at waiting for a group of tasks?
- j88439h84 6y agoI'm on mobile so cant type a long response but IME its quite different in terms of ease of use, complexity, and correctness. For a theoretical take see https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/ https://vorpus.org/blog/notes-on-structured-concurrency-or-g... To be precise, to wait for a task its actually just await. Theres no ensure_future etc. Trio doesn't have the concept of futures or promises at all, and await f() is treated as a single piece of syntax, so in practice it doesn't have "awaitables" either.
- nerdwaller 6y agoFair enough, the ensure_future() vs create_task() has confused me more than once. It would have been nice if there was a different namespace for the lower level API. Even so, the docs aren’t even clear on when which of the two is appropriate: https://docs.python.org/3/library/asyncio-task.html#asyncio... https://docs.python.org/3/library/asyncio-task.html#asyncio..... https://docs.python.org/3/library/asyncio-future.html#asynci.. https://docs.python.org/3/library/asyncio-future.html#asynci....
- naasking 6y agoAn interesting idea, essentially Trio restructures concurrent code to more closely resemble parallel code, which innately has the property that the concurrency is not observable, ie. the black box property. It's a little more general than strict parallel code though because task spawning is reified as a first class value via which the program can spawn new tasks. I'm not sure it's totally novel though. For instance, C# has AsParallel() extensions which let you run collection operations in parallel (similar functions in Haskell too). It has the same black box behaviour described by the article, and there's an equivalence between direct control flow in code and indirect control flow reified as a data structure, ie. Haskell's case that lazy evaluation and lists are the only control flow construct you need. Still, it's an interesting imperative incarnation of the idea!
- 0az 6y agoStructured concurrency is why you should use Trio. What do you get? For starters, Ctrl-C/KeyboardInterrupt just works. By restricting the design space, you end up with much more intuitive designs. Obligatory Structured Concurrency essay link: https://vorpus.org/blog/notes-on-structured-concurrency-or-go-statement-considered-harmful/ https://vorpus.org/blog/notes-on-structured-concurrency-or-g... I recommend giving it a read.