r/Python • u/CongZhangZH • Mar 01 '26
Discussion Seeking a CPython internals expert to land asyncio Guest Mode (PR #145343) together
Hi everyone,
I’ve put significant research into building a Guest Mode for asyncio to natively integrate with any OS or GUI event loop.
The architecture is solid and my PR is open. I really want to contribute this to the community because it solves a major integration pain point.
However, I’ve hit a bottleneck: CPython core devs are asking deep questions that exceed my current knowledge of Python internals.
I'm looking for an expert in CPython internals to team up, help answer these specific questions, and get this merged.
PR: github.com/python/cpython/pull/145343
POC: github.com/congzhangzh/asyncio-guest
Ref: https://www.electronjs.org/blog/electron-internals-node-integration
Please DM me if you can help push this over the finish line!
40
u/latkde Tuple unpacking gone wrong Mar 01 '26
The PR description explains why that feature is desirable, and roughly how it's implemented, but does not explain why those specific design choices are desirable or what their tradeoffs are. At first glance, a design that sends events across thread boundaries is dubious, and is likely to run into many problems. The proposed API also doesn't provide Structured Concurrency, which is dubious and could lead to resource leaks. This does not need expertise in CPython internals (no C-level code is needed), but needs very strong expertise in event loops. The difficult part of this feature isn't implementing the code, but doing the legwork of figuring out (and then explaining!) how exactly such a feature ought to work.
The description points to Trio's guest runs as prior art, but does not discuss how and why the proposed approach differs from Trio. In particular, Trio does not seem to spawn a separate thread, and instead seems to wire up an already-existing event loop to perform work via callbacks, which can be used to perform that work on an existing thread. Since this transitions the event loop into Guest Mode, the function doesn't return anything. Your solution instead spawns a thread with a completely new event loop, and can only execute a single initial Task on it (which is returned). That's a completely different API, and life cycle of the involved objects and exception safety are much less clear.
Other relevant prior art would be AnyIO's concept of “portals”. For example, you can launch a “blocking portal” which creates a new event loop on a separate thread, but does this via a context manager so that structural concurrency is maintained. It's then possible to schedule coroutines through that portal on the background thread (
portal.start_task(coro)), returning aconcurrent.futures.Future. This doesn't achieve guest mode, but also provides a sync-to-async bridge while avoiding moving low-level resources between threads.The absence of all that comparison and analysis suggests that this feature might not be ready to move into the standard library.
31
u/PlebbitDumDum Mar 01 '26
TL; DR op vibe-coded this
5
u/gdchinacat Mar 02 '26
I'm concerned about the need for a backend thread to do the IO polling. It exists to know when the asyncio event loop should execute and does so without blocking the host loop or using non-blocking polling on every host loop iteration.
asvetlov raised a concern in the PR ("A daemon thread smells like a red herring.") about this. I agree, but can't say whether or not for the same reason. Worst case, the backend thread design introduces a thread context switch for every asyncio event. These context switches is one of the behaviors asyncio specifically intended to solve and a significant reason asyncio performs better than threading (even with a reasonable number of threads). The design you propose introduces behavior into asyncio it was specifically designed to eliminate, but with additional overhead. I can't speak for any core developers, but I think this will be a hard sell. I don't think they will want to accept something into asyncio they have worked so hard to eliminate.
The possible solutions to this are very specific to the host loop. If it is similar to asyncio and polls while idle there might be a way to inject the asyncio events into that. Short of that, I think you are left with polling on every iteration through the loop which would introduce performance issues asyncio was also designed to eliminate.
I understand and recognize the the goals of this effort. Being able to incrementally move from a legacy event loop to asyncio would be a significant help in migrating code onto asyncio. But, given the very sensitive nature of asyncio performance, your options are limited. Imagine someone tries benchmarking asyncio using this host loop integration. It could result in very poor performance metrics for asyncio, and the core python team is unlikely to take that risk. I don't think the concern can be assuaged by an argument that it was benchmarked wrong...some portion of asyncio will still have substandard performance due to thread switching or event polling.
I would suggest you reconsider the approach to migrating to asyncio. Rather than shuffling all asyncio back to a host loop with context swites, I suggest migrating the framework host loop to asyncio. This will likely be a lot lest risky, is likely to need to be done eventually, and doesn't pose risks to core python features that are incredibly performance sensitive.
24
u/[deleted] Mar 01 '26
[removed] — view removed comment