Concerning Trio - Nathaniels example for comparing this with other libraries, which apparently should be simple and in few lines of code, is implementing the happy-eyeballs algorithm: Nathaniel J Smith - Python Concurrency for Mere Mortals - Pyninsula #10 - YouTube
This algorithm can be written using Lwt in a more readable style, and in fewer lines of code - which uses the cancellation semantics of Lwt via Lwt.pick
. Though the solution is a bit errorprone because of the never
thread (this problem is present with nurseries too): Structured-concurrency libraries - #17 by rand
Nurseries seem equivalent to a concurrency monad - but where there is no way to start a thread except to combine it into the monad using a concurrency operator. Concretely, the nursery in Trio is just Lwt.choose
.
So to make Lwt into structured concurrency: disallow Lwt.async
and let running_thread = run () in <code hopefully using running_thread>
. In this alternative semantics of Lwt - run ()
would never run if not combined into the returned monadic value, and would just be GC’d.
As I understand it - monads in Haskell work exactly like this - where a monadic value has no identity, and cannot be run right away, but is instead a recipe for a computation. This would be a safer solution than Lwt, and I guess, safer than nurseries too.
Edit: Would be interesting to have equivalent implementations of happy eyeballs in Eio
, Lwt
and Haskell
to compare!