Tinyio: A tiny (~200 line) event loop for Python

https://news.ycombinator.com/rss Hits: 9
Summary

tinyio A tiny (~200 lines) event loop for Python Ever used asyncio and wished you hadn't? tinyio is a dead-simple event loop for Python, born out of my frustration with trying to get robust error handling with asyncio . (I'm not the only one running into its sharp corners: link1, link2.) This is an alternative for the simple use-cases, where you just need an event loop, and want to crash the whole thing if anything goes wrong. (Raising an exception in every coroutine so it can clean up its resources.) import tinyio def slow_add_one ( x : int ): yield tinyio . sleep ( 1 ) return x + 1 def foo (): four , five = yield [ slow_add_one ( 3 ), slow_add_one ( 4 )] return four , five loop = tinyio . Loop () out = loop . run ( foo ()) assert out == ( 4 , 5 ) Somewhat unusually, our syntax uses yield rather than await , but the behaviour is the same. Await another coroutine with yield coro . Await on multiple with yield [coro1, coro2, ...] (a 'gather' in asyncio terminology; a 'nursery' in trio terminology). rather than , but the behaviour is the same. Await another coroutine with . Await on multiple with (a 'gather' in asyncio terminology; a 'nursery' in trio terminology). An error in one coroutine will cancel all coroutines across the entire event loop. If the erroring coroutine is sequentially depended on by a chain of other coroutines, then we chain their tracebacks for easier debugging. Errors even propagate to and from synchronous operations ran in threads. Can nest tinyio loops inside each other, none of this one-per-thread business. Ludicrously simple. No need for futures, tasks, etc. Here's the full API: tinyio . Loop tinyio . run_in_thread tinyio . sleep tinyio . CancelledError Installation pip install tinyio Documentation Loops Create a loop with tinyio.Loop() . It has a single method, .run(coro) , which consumes a coroutine, and which returns the output of that coroutine. Coroutines can yield three possible things: yield : yield nothing, this just pauses and gives ...

First seen: 2025-07-26 23:16

Last seen: 2025-07-27 07:21