Skip to content

Conversation

@patrick-kidger
Copy link
Owner

@patrick-kidger patrick-kidger commented Jan 16, 2026

Supersedes #16 – credit @aburgm for spotting the underlying error! In this PR I've kept @aburgm's test to validate the fix I've applied.

waits, thunks, loops

@aburgm, you were asking about why we had wait wrapped into a thunk, rather than being called directly. The reason for this is tinyio.to_{asyncio,trio}. The use-case here is when someone has written a tinyio coroutine, but the available event loop is from asyncio/trio. In this case what we need to do is spin up a tinyio loop, but tie its stepping to that of the host event loop – i.e. we want to write an asyncio/trio coroutine which steps the tinyio loop at each await point.

If the tinyio loop finds itself sat waiting, then we need to be able to simultaneously (a) wake the tinyio loop back up once the event triggers, but also (b) allow the host event loop to keep stepping in the mean time. To do this, we move the tinyio event-watching to a thread so that we can do both things simultaneously. In order to perform just the event watching in the thread, we need to wrap that operation in a thunk we can schedule on a thread.

other

This PR also swaps out threading.Event for a cross-process compatible choice. Right now this should be invisible to users, but in the future it should enable us to asynchronously launch subprocesses too.

- current_coro_ref -> current_coro in the core loop
- renamed a few variables
… is in anticipation of adding e.g. subprocess calls.
@@ -1,5 +1,5 @@
<h1 align="center">tinyio</h1>
<h2 align="center">A tiny (~300 lines) event loop for Python</h2>
<h2 align="center">A tiny (~400 lines) event loop for Python</h2>
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

D:

we started at 200, what is the world coming to.

Maybe it's time to rename to mediumio

@patrick-kidger patrick-kidger merged commit bf82575 into main Jan 16, 2026
2 checks passed
@patrick-kidger patrick-kidger deleted the ki-wait branch January 16, 2026 11:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants