What's the difference between loop.create_task, asyncio.async/ensure_future and Task?

后端 未结 1 1013
天涯浪人
天涯浪人 2021-02-01 02:49

I\'m a little bit confused by some asyncio functions. I see there is BaseEventLoop.create_task(coro) function to schedule a co-routine. The documentation for

相关标签:
1条回答
  • 2021-02-01 03:38

    As you've noticed, they all do the same thing.

    asyncio.async had to be replaced with asyncio.ensure_future because in Python >= 3.5, async has been made a keyword[1].

    create_task's raison d'etre[2]:

    Third-party event loops can use their own subclass of Task for interoperability. In this case, the result type is a subclass of Task.

    And this also means you should not create a Task directly, because different event loops might have different ways of creating a "Task".

    Edit

    Another important difference is that in addition to accepting coroutines, ensure_future also accepts any awaitable object; create_task on the other hand just accepts coroutines.

    0 讨论(0)
提交回复
热议问题