Skip to content
Advertisement

AttributeError: module ‘asyncio’ has no attribute ‘create_task’

I’m trying to asyncio.create_task() but I’m dealing with this error:

Here’s an example:

JavaScript

Out:

JavaScript

So I tried with the following code snippet (.ensure_future()) instead, without any problem:

JavaScript

Out:

JavaScript

What’s wrong?


[NOTE]:

  • Python 3.6
  • Ubuntu 16.04

[UPDATE]:

With borrowing from @user4815162342 Answer, my problem solved:

JavaScript

Advertisement

Answer

The create_task top-level function was added in Python 3.7, and you are using Python 3.6. Prior to 3.7, create_task was only available as a method on the event loop, so you can invoke it like that:

JavaScript

That works in both 3.6 and 3.7, as well as in earlier versions. asyncio.ensure_future will work as well, but when you know you’re dealing with a coroutine, create_task is more explicit and is the preferred option.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement