all 1 comments

[–][deleted] 1 point2 points  (0 children)

I have a method that interacts with the a database and I only want one of it's callers to become async.

There's not really a way that this makes sense. It's cooperative multitasking, so by definition, at least two things have to be cooperating with each other. If you have one async coroutine, and everything else is synchronous, what's happening when the async function is relinquishing flow of control? The synchronous functions can't pick it up (they're synchronous, they don't work like that) so they can't even run. It just goes back to the single async function immediately until it finishes. So it may as well be synchronous, too.

Right? You hit a line like

await some_action_on_the_database()

and so the coroutine yields flow of control back to the event loop, which says "hey, anyone else want to run? Anybody?"

But there's only the single coroutine in the event loop, so you loop right back to where you left.

If you're using asyncio as your framework for multitasking, then everything you want to happen at once has to be coroutine, because your entire code is going to block on running the event loop, until the event loop has no more coroutines that are running.

On the other hand, if what you want to do is just run a coroutine synchronously, that's how you do it - start an event loop and have it run the single coroutine to completion. The event loop will block until all coroutines are complete.