Can The Async Eventloop Switch Between Coroutines On The Await Call Itself?
Solution 1:
The event loop can't switch to other coroutines await
unless the awaited coroutine (or other awaitable) chooses to suspend. Since some_slow_method
doesn't contain a single await
, it will never suspend and therefore the_caller()
will never switch to another coroutine. The fact that the event loop doesn't always suspend on await
is sometimes a source of bugs, as discussed here.
You can force some_slow_method
to occasionally switch to other coroutines using await asyncio.sleep(0)
, but that is considered bad style because the asyncio thread should avoid doing CPU work. If you must do CPU-bound work, a better option is to make some_slow_method
an ordinary def
and run it using await loop.run_in_executor(None, some_slow)
. This will hand it off to a thread pool, suspend the current coroutine, and wake it up once the function has completed, transferring its return value (or exception if one was raised).
Post a Comment for "Can The Async Eventloop Switch Between Coroutines On The Await Call Itself?"