Issue
What's the difference between these functions
create_task
andcall_soon
call_soon_threadsafe
andrun_coroutine_threadsafe
create_task vs call_soon
these two functions schedule the execution of a Coroutines, not thread-safe.
create_task
returns a Task
object, and call_soon
returns Handle
object, but is there any difference other than this, especially in usage?
call_soon_threadsafe vs run_coroutine_threadsafe
these two functions are meant to be called from a different OS thread than the one where the event loop is running. And in most cases, I can use any of them.
Solution
create_task
vscall_soon
- these two functions schedule the execution of a Coroutines
No, only create_task
schedules the execution of a coroutine (async function). call_soon
schedules the one-shot execution of an ordinary function.
So, if you have a coroutine x
defined with async def
that you want executed "in the background", you call asyncio.create_task(x())
. If you have a function f
defined with def
that you want executed as soon as the event loop is next idle, you call loop.call_soon(f)
.
The same difference in usage applies to run_coroutine_threadsafe
and call_soon_threadsafe
, which allow submitting work to a running event loop from a different thread.
Answered By - user4815162342
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.