Search code examples
pythonpython-asynciomicropython

Why does uasyncio module in micropython doesn't have any "run" function?


I am using an ESP8266 board with micropython.

I tried to use asyncio, but it doesn't work and says me that :

Traceback (most recent call last):
  File "<stdin>", line 10, in <module>
  File "<stdin>", line 7, in main
AttributeError: 'module' object has no attribute 'run'

I took a look about what does uasyncio contains and I got this :

>>> import uasyncio as asyncio
>>> asyncio.
__class__       __name__        __path__        DEBUG
log             select          sleep           sleep_ms
time            ucollections    uerrno          utimeq
type_gen        set_debug       CancelledError  TimeoutError
EventLoop       SysCall         SysCall1        StopLoop
IORead          IOWrite         IOReadDone      IOWriteDone
get_event_loop  SleepMs         cancel          TimeoutObj
wait_for_ms     wait_for        coroutine       ensure_future
Task            _socket         PollEventLoop   StreamReader
StreamWriter    open_connection                 start_server
uasyncio        core

After some researches, I have found in this documentation that uasyncio is supposed to get functions like new_event_loop(), run, etc... that I absolutely don't have there...

There is my script :

from robot import *
import uasyncio as asyncio

def main(args):
    robot = MicroportalRobot("robotName", "serverAddr", 8266)
    asyncio.run(robot.main())

if __name__ == "__main__":
    main([])

Why?


Solution

  • The features you're looking for are part of uasyncio v3, released as part of micropython 1.13. You need a micropython version at least as recent as 1.13 to use them. Your comments indicate you're on micropython 1.11, meaning you have the old v2 uasyncio implementation, which does not have these features.

    You will need to install a more recent micropython build, or stick to the uasyncio v2 API.