trio
trio copied to clipboard
Add `__main__` module with `await`-compatible console
I recently learned that python -m asyncio
drops you into an
interpreter session that knows how to deal with await
expressions. For
example,
$ python -m asyncio
asyncio REPL 3.10.6 [...] on darwin
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> await asyncio.sleep(1); print("hi") # prints after one second
hi
This quickly became an important part of my async debugging toolbox.
I've also been learning Trio recently, but I've been missing my async console.
This PR takes the script from asyncio.__main__
and adapts it to
use a Trio run loop. With this patch applied, users can run python -m trio
to drop into an interactive interpreter session that lets them
directly await
Trio-based async expressions.
$ python -m trio
Trio 0.21.0+dev, Python 3.10.6 [...] on darwin
Use "await" directly instead of "trio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import trio
>>> await trio.sleep(1); print("hi") # prints after one second
hi
I'm very new to Trio, so I'm sure my approach is suboptimal. In particular, I'd really appreciate feedback from the team/ community on:
- not using
threading
with a brand newtrio.run
to evaluate awaitable expressions. I found that if I simply usedself.nursery.start_soon
in the same thread asruncode
,await
expressions wouldn't get the chance to be evaluated until the session was shutting down (might have been blocked by reading keyboard input, if I had to guess). Could there be a solution intrio
's threading tools? - what documentation updates should accompany this PR
- what sort of tests I should add (since this change implements a brand
new code path that won't be touched by any existing or future usage of
trio
itself, I wasn't sure if it would even be appropriate to try and test it) - anything else to align this addition to the Trio way!
Many thanks for your consideration.
Codecov Report
Attention: 48 lines
in your changes are missing coverage. Please review.
Comparison is base (
78c55aa
) 99.64% compared to head (a43a9f3
) 99.37%.
Additional details and impacted files
@@ Coverage Diff @@
## master #2407 +/- ##
==========================================
- Coverage 99.64% 99.37% -0.28%
==========================================
Files 117 118 +1
Lines 17634 17682 +48
Branches 3172 3180 +8
==========================================
Hits 17572 17572
- Misses 43 91 +48
Partials 19 19
Files | Coverage Δ | |
---|---|---|
src/trio/__main__.py | 0.00% <0.00%> (ø) |
Given that #2972 has been merged, this PR should be closed, shouldn't it? (With thanks to @wbadart for initiating it, and to @CoolCat467 for pushing it forward.)
Yeah it should have been. Thanks for the contribution nonetheless!
Glad to see this feature has been added, and I'm happy to have played a (small) role in the solution!