Text of discuss.python.org › t › asyncio-without-function-colouring › 106614 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Asyncio without function colouring - Ideas - Discussions on Python.org

Asyncio without function colouring Ideas JonathanRoach (Jonathan Roach) March 20, 2026, 11:51am 1 If you want to await you need to be in an async def function. I propose changing Python to allow await , async for and async with in any function. def myfunc(): await myasyncfunc() # same as if you're in an async function await myfuture # inside a Task the future is awaited; outside, an Exception is raised async for myasyncgenerator: # same as if you're in an async function ... async with with_item: # same as if you're in an async function ...

Why this is helpful: if you use async you cannot make good use of async within Python’s constructs like @property , __getattr__() etc, C-implemented functions like len() and setattr() , and operator overrides like __lshift__() as you have to wait for for the async result before returning, which negates most of the point of async , to be able to do other, useful work while waiting. Also, any library which wants to support async and non- async has to be written twice - once async and once without.

These together have made async only useful in niche cases, in particular not for web sites, the highlighted use case async was added for. (aside: I tried using async in a Django website, and concluded it was unusable in its current form, which is why I decided to try and improve Python). I’ve taken the trouble to create a proof-of-concept cpython which does this: GitHub - JonathanRoach/cpython-await-anywhere: The Python programming language, with await allowed anywhere · GitHub (await-anywhere branch).

I’ve tested this ( make test without any fails) on Mac and Linux, debug, non-debug and optimised, but not Windows yet. Any feedback is very welcome (even ‘it doesn’t work’). The only new assumptions (over cpython) are that C standard setjmp() and longjmp() are available. I am seeking a sponsor for a pep for this ( @python /pep-editors please could you help me find one). There is plenty more detail (eg ‘how?’ and ‘what are the catches?’), but lets get the community’s views first, over to you… Add Virtual Threads to Python Rosuav (Chris Angelico) March 20, 2026, 11:58am 2 First have a discussion.

Then if a core dev is expressing support, you can ask him/her to consider sponsoring the PEP. And to have that discussion, you have to explain what the exact semantics of await in a non-async function are. How does that work? 1 Like JoBe (jobe) March 20, 2026, 2:43pm 3 I think the basic concept behind this has been discussed before, although I don’t remember where exactly. I myself have often wondered about the same situation you were facing, basically mixing async and non async code in a function (or module namespace). jacopoabramo (Jacopo Abramo) March 20, 2026, 3:14pm 4 I think it probably happened in this discussion about virtual threads .

1 Like JonathanRoach (Jonathan Roach) March 20, 2026, 3:19pm 5 It has. I contributed to that discussion, but I really I was more narrowly focussed on avoiding the function colouring problem of asyncio , so I put this narrower idea up for discussion. JonathanRoach (Jonathan Roach) March 20, 2026, 4:05pm 6 await myfunction_async() . From the Python programmer’s POV this is a function call. You call myfunction_async() the value of the await is the return value from the function.

This is the case in async and non- async functions. Behind the scenes, its operation is much the same: when possible, the function call is inlined, otherwise .send() is used, exactly the same as python works now. However, .send() on a coroutine (thing returned by calling an async function) now sets up a C and Python stack. The C stack management is handled by Python/coroutine.c, the Python stack is simply the Datastack from Python, but as a thing in its own right.

TBH, if you don’t have to await a function, you will be better off not as await myfuntion_async() is noticeably slower and takes more typing than myfunction_sync() . In the proof-of-concept the only time you must async def is for a Coroutine to pass to asyncio.Task() , but the proof-of-concept could be extended to accept a callable to asyncio.Task . await future . In a asyncio.Task the future waits, like now. Outside an asycio.Task an Exception is raised.

Behind the scenes, Future.__await__() is re-engineered to behave to the caller as if it only ever completes (return value, or exception), to yield itself to the Coroutine’s .send() it calls a new function Coroutine.doyield() (a new function) which uses the Python/coroutine.c yield mechanism to preserve the C execution state, wrapped in code to preserve the Python state and yield the Future to the caller of .send() .

As far as Python code is aware the behaviour is unchanged (unless you’ve rolled your own Future -like class). You can now yield in C using _PyCoro_DoYield() . Yielding and sending are much more efficient as they avoid passing the Future up the Python stack, and the .send() value down the stack - the C simply continues from where it yielded. For almost all Python coders, everything works the same, except you don’t need async def , async for and async with (they work, you just don’t need them, IMO use the sync ones they’re less typing and faster).

There are exceptions: Future awaiting is implemented differently (see above) - this will affect home-brew Future classes; (x async for x in myasyncgenerator()) is now a sync generator, not an async one; KeyboardInterrupt now behaves the same in an async.Task s as in sync code; await and async aren’t syntax errors in sync functions. JonathanRoach (Jonathan Roach) March 20, 2026, 4:33pm 7 See my earlier reply for the Python side - please ask if you want more detail in any area.

Here, I’ll focus on the C side here. The goal was C coroutines without needing a special coding style by the user and only using standard C libraries. Only setjmp() and longjmp() are new to cpython, and these are widely available C standard libraries. There is no assembler. The approach is to manage the C stack as a memory heap. The start of each block is the stack frame of a routine with a setjmp() recorded to re-enter that routine.

These are the entry points to coroutines, called here entry_point() . To divide up the stack entry_point() calls another routine, called here use_some_stack() . use_some_stack() uses alloca() to allocate some stack before calling entry_point() . The stack now looks like this: <(A) entry_point()><(B) use_some_stack(N) with N bytes of alloca()-allocated space><(C) entry_point()>. (A) has N bytes (and a little more) of stack headroom before bumping into (C).

An coroutine needing N bytes or less can longjmp() to (A) and run knowing it has the headroom - this is how coroutines are entered. When a coroutine yields the stack looks like this: <(A) entry_point()><coroutine doing stuff……><(D) yield_here()><(C) entry_point()>. yield_here() uses setjmp() to allow a re-entry when the coroutine resumes. For managing the heap/coroutines ' entry_point() can: Chunk_Create : alloca() some space and call entry_point() Chunk_Split : divide the space between this entry_point() and the next one into two pieces.

Chunk_Enter : start a coroutine and yield here() can: Chunk_Create : alloca() some space and call entry_point() Chunk_Enter : resume the coroutine. See Python/coroutine.c for the implementation in the proof-of-concept. The heap is a merge-on-free, address ordered, first-fit style of heap, managed using linked lists. This kind of heap works very well up to about 50 free blocks, and should be a good fit for Python C stack fragments.

Liz (Elizabeth King) March 23, 2026, 3:13am 8 What do you think it means to be the “same as in an async function”? If this is accurate, this is worse than the status quo. It means we go from clearly defined function coloring, to just hiding the function color and sometimes things will fail, or maybe even just deadlock depending on the reasonable assumptions something was written with. JonathanR

The rest of this page is on the live site.