From my experience working on asyncio code and sync/non-asyncio code, probablythe most annoying part is actually building interfaces that support both. You get a bunch of aget or async_get verus get calls. Async adapters and everything else. Tracking what can be used inside an async context and what cannot be.
Since I have been in Django world again recent, Django recently implemented the task framework, which is just a unified interface for defining background task in Django. That really got me to thinking, how do we unify the API for asyncio/sync code? Which led to what about concurrency in general?
What if async/coroutines “just worked” in Python? Without you needing to set up an event loop or run asyncio.run?
Very similar to how the task framework for Django works, what we instead had similar concurrency backend/configuration in Python that could be configured?
So for asyncio:
# yes I know, I should be using `asyncio.run` or a runner, just an interface example
import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(coroutine)
Might become something like:
import concurrency
concurrency.set_backend(concurrency.Asyncio)
await coroutine
But why?
This would allow automatic handling of async/coroutines. Which would unify the concurrency interface. No longer needing to make async adapter or everything else to support both sync and async. The default concurrency backend might be “immediate”, which is just “execute coroutine as a sync function”. So, this mean if you want to support asyncio, you just implement async def functions and it should “just work”.
But, it would also allow other concurrency abstracts.
So instead of:
import threading
thread = threading.Thread(function)
thread.start()
thread.join()
You could have:
import concurrency
concurrency.set_backend(concurrency.Threading)
await coroutine # automatically creates background thread and runs coroutine in thread
So, this could have the advantage of unifying the interface between asyncio/non-asyncio code and threading code. With the popularity of free-threading and how that change is really going to impact the ecosystem once it is rolled out, shifting the devex/interface could make support all of the concurrency methods a lot easier.