-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API: highlevel strategy discussion #42
Comments
A few more questions that didn't fit into the above:
|
Thanks for the write up! I don't know what you're missing and never used trio-asyncio, but if your premises are correct, I do agree with your conclusions: |
In my opinion, the API should be simply the The basic primitives should be exposed, but like, not the way to do anything unless you need them. This would be, in my opinion, the simplest and easiest way to do everything. |
@njsmith You're right in that The |
@smurfix Oof, I hear you on that :-(. Hope you're feeling better, and good luck. |
I guess this will break in the future as we work through python-trio/trio-asyncio#42 but right now I don't care I just want these freaking tests to pass.
It looks like the three primitives we need to implement coroutine runner switching are:
On the Trio side, we control everything, so these are all pretty straightforward to implement. On the asyncio side I think we can implement these as:
A first attempt would be to (a)
Alternatively we could try mutating We should also check So we might want to make sure These options are all pretty terrible; if we go ahead with this we should talk to the asyncio folks about making sure that in 3.8 there's a nicer API for this, or at least that our terrible hacks don't break, to avoid a repeat of #23. BUT, it does look like there aren't any show-stoppers currently, so we should probably prototype this out and decide whether we're actually going to commit to it before we talk to the asyncio folks about it. |
You know what else might help makes things less confusing? We should make it so that if you try to enter trio-mode when you're already in trio-mode, or try to enter aio-mode when you're already in aio-mode, then that's fine and just a no-op. That switches the mindset from "mark the places where you transition", to a mindset of "mark what you need in each place". And in particular if you write something like @trio_mode
async def blah(...):
... then (It's actually possible this is already how it works? But the |
Yeah, I had that idea originally, but postponed it – as it turns out it's 3.7+ only, because it needs context management from asyncio. |
But, if we're using our implementation of the loop, surely we can make sure to set |
Tried that. Ran into a couple of interesting cases where this is not as easy as it seems. |
I made this https://gist.github.com/graingert/d20fdaa41511c4cccb756259ee477444#file-trio_in_asyncio-py-L149-L158 does that help? |
To do this you only need to raise StopIteration out of the Coroutine you pass to asyncio.create_task |
I feel like trio-asyncio's core functionality is been pretty solid for a while, but we're still flailing a bit trying to find the most usable way to expose it to users. I guess right now it has 3 different APIs, we're discussing a 4th, and it's not clear whether any of them is actually what we want? And I'm frustrated that I don't feel like I understand what the pitfalls are or what features users actually need. And my main coping strategy for that kind of frustration is to open an issue and write a bunch of text to organize my (our?) thoughts, so here we go. Hopefully laying out all the information in one place will give us a clearer picture of what the trade-offs actually are, and help us find the best way forward. (4 overlapping APIs and constant churn cannot be the solution!)
What I think I know
Strategies we've tried:
run_asyncio
,run_future
,run_trio
: explicit call-in-other-mode transitions, inspired bytrio.run
await whatever()
is shorthand forcoro = whatever(); await coro
. It turns out that there is some asyncio code (in aiohttp? which code?), that callsasyncio.current_task()
from inside thecoro = whatever()
part, not within theawait coro
part. This breaks a naive implementation ofrun_asyncio
, that only runs theawait coro
part inside anasyncio.Task
. However, now that we know about this, it's easy to fix by makingrun_asyncio
perform both halves of theawait whatever()
dance inside the new task. (Code like this is also broken in pure asyncio if you doasyncio.create_task(whatever())
, but never mind that...)aio_as_trio
,trio_as_aio
: translators for common protocols (specifically: CM, async CM, iterable, async iterable, and async callable – but not sync callable).aio_as_trio(fn)(...)
is going to confuse the heck out of newcomers ("Readability counts")asyncio.current_task
, and it turns out that people (aiohttp) call this annoyingly often. And... you can't really spawn a task just to callasyncio.current_task
, that's not going to give useful results; the task it returns will be gone before you can do anything with it.async_timeout
, assume that their__aenter__
,__aexit__
, and body all execute in the same task. That's not true for a naive implementation ofaio_as_trio
that just usesrun_asyncio
to call the__aenter__
and__aexit__
. Is a non-naive implementation even possible? I don't see how...trio_as_aio
allow_asyncio
: use some Clever Coroutine Tricks to create a hybrid asyncio/trio mode where you can call either kind of function.asyncio.CancelledError
<->trio.Cancelled
at the boundary. It's not clear how bad this is in practice... mainly it means thattrio.Cancelled
might pass through asyncio code. This will probably be treated like a generic unhandled exception, which in many cases will be what you want. Grepping aiohttp, there are a number of places that look forasyncio.CancelledError
, but they mostly seem to be ad hoc attempts to implement something like Trio's cancel scope delimination, so maybe we already handle that fine?run_trio
withallow_asyncio
would do it though.current_task
doesn't work at all. This breaks the popularasync_timeout
library (used by e.g. aiohttp, homeassistant, and others). This seems like a showstopper problem. If we can't fix this, then I don't think we can in good conscience offer "hybrid mode" as a feature. Even if we document that it only works in "simple cases", then the first thing people will try is writing a simple little program... that uses aiohttp, and it won't work.async with aio_mode
,async with trio_mode
, or maybe evenasync with hybrid_mode
: Hypothetical approach that would let you switch modes in the middle of a function (details: Should we have a way to let some other coroutine runner take temporary control of a task? trio#649).run_asyncio
/run_trio
, but with better ergonomics because you don't need to define and call another function.Some tentative conclusions?
The
allow_asyncio
hybrid-mode approach is never going to be 100% reliable (because of the cancellation issue), and currently is pretty badly broken (because of thecurrent_task
issue making it incompatible with super-popular libraries likeaiohttp
). Conclusion: it will never be the only option (we want to give people the option of using something less magical and more reliable when they have to), and right now we probably shouldn't be shipping it at all. So let's put it aside for the moment and focus on the other options.From an API design standpoint, I think it makes sense to provide the basic
run_asyncio
,run_future
,run_trio
primitives. They aren't necessarily the thing we expect people to use all the time, but they provide a set of simple, reliable core primitives that you can always fall back to if you have some confusing situation where our more ergonomic options don't work. Alternatively, if we getasync with aio_mode
/async with trio_mode
working, those could also serve as a set of basic primitives.The
aio_as_trio
/trio_as_aio
are... maybe not actually a good idea after all? That's not the conclusion I was expecting to reach; I thought I was going to end up arguing for them as convenience shorthand on top of therun_*
primitives. But I'm really concerned about the issues caused by running__aenter__
and__aexit__
in different tasks – that really will break all kinds of stuff. Theasync with aio_mode
/async with trio_mode
approach avoids this problem, e.g.:So....... maybe I've argued myself into thinking that
async with aio_mode
/async with trio_mode
really are something we have to dig into more, and might even be The Solution To All Our Problems? There are still a bunch of details to work out first to figure out how these can work, but maybe we should do that.They even make a reasonable substitute for
@aio2trio
/@trio2aio
, e.g. instead ofYou write
One extra level of indentation, but the same number of lines, and no need for anything really annoying like writing a trampoline function.
What am I missing?
Probably a bunch of stuff, but hopefully laying out my thinking will make it obvious to someone what terrible mistakes I'm making? Please help me be smarter :-)
The text was updated successfully, but these errors were encountered: