Jul 292023

So, you’re doing some sync stuff.
But you also need to do some async stuff,
without making everything async.
Maybe the sync stuff is an existing application.
Maybe you still want to use your favorite sync library.
Or maybe you need just a little async,
without having to pay the full price.
Of course,
you can run a coroutine with asyncio.run(),
and blocking sync code from a coroutine with asyncio.to_thread(),
but the former isn’t granular enough,
and the latter doesn’t solve async code being at the top.
As always, there must be a better way.
Maybe something like this?

async def do_stuff(i): return i * 2 async def generate_stuff(i): for n in range(i): yield n runner = ThreadRunner()
print(runner.run(do_stuff(2))) # 8
print(*runner.wrap_iter(generate_stuff(4))) # 0 1 2 3

Now, it would take a long…

External feed Read More at the Source: https://death.andgravity.com/asyncio-bridge


Sorry, the comment form is closed at this time.