uasyncio deque problem

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
Jim.S
Posts: 84
Joined: Fri Dec 18, 2015 7:36 pm

uasyncio deque problem

Post by Jim.S » Sun Feb 18, 2018 5:35 pm

I am just getting back into programming python on the pyboard after 14 months, so I accept I may be omitting something obvious.

I am attempting to learn how ascyncio works, and have copied the example from section 7.3 the uasyncho approach of the tutorial (see below). I have successfully copied the uasyncio module and also the collections/deque module from the micropyton-lib (using upip) to the sd card on my pyboard.

I am using the latest standard firmware i.e. MicroPython v1.9.3 on 2017-11-01; PYBLITEv1.0 with STM32F411RE)

Now when I run the code below I get the following error,
Traceback (most recent call last):
File "main.py", line 39, in <module>
File "main.py", line 39, in <listcomp>
File "main.py", line 11, in __init__
File "uasyncio/core.py", line 224, in get_event_loop
File "uasyncio/__init__.py", line 21, in __init__
File "uasyncio/core.py", line 30, in __init__
AttributeError: 'module' object has no attribute 'deque'

The error seems to be to do with the following line

Code: Select all

loop = asyncio.get_event_loop()
I have tried various ways to get the code to see the deque module (see code snippet) but I have now run out of ideas.

Code: Select all

import pyb
import uasyncio as asyncio
from collections import * #my addition to example code
#copied from "7.3 the uasyncho approach"
class LED_async():
    def __init__(self, led_no):
        self.led = pyb.LED(led_no)
        self.rate = 0
        loop = asyncio.get_event_loop()
        loop.create_task(self.run())

    async def run(self):
        while True:
            if self.rate <= 0:
                await asyncio.sleep_ms(200)
            else:
                self.led.toggle()
                await asyncio.sleep_ms(int(500 / self.rate))

    def flash(self, rate):
        self.rate = rate

    def on(self):
        self.led.on()
        self.rate = 0

    def off(self):
        self.led.off()
        self.rate = 0

async def killer():
    sw = pyb.Switch()
    while not sw.value():
        await asyncio.sleep_ms(100)

leds = [LED_async(n) for n in range(1, 4)]
for n, led in enumerate(leds):
    led.flash(0.7 + n/4)
 
loop = asyncio.get_event_loop()## this line is problematic
loop.run_until_complete(killer())

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uasyncio deque problem

Post by pythoncoder » Mon Feb 19, 2018 7:11 am

You are using the uasyncio version from the @pfalcon fork. My tutorial and libraries support the official repository only. Unfortunately upip currently loads the @pfalcon version.

Please re-install using the instructions in the tutorial. You may have an old version of this: I recently updated it because of this split.
Peter Hinch
Index to my micropython libraries.

Jim.S
Posts: 84
Joined: Fri Dec 18, 2015 7:36 pm

Re: uasyncio deque problem

Post by Jim.S » Mon Feb 19, 2018 9:39 pm

Thanks! I sort of understand and have got it to work. I created a directory on my pyboard file system called uasyncio and manually downloaded __init__.py , core.py, queues.py and synchro.py (from the various uasyncio directories on git hub) into it.

I'm assuming that if I knew how to use "git" properly there is an easier way, but this worked for now. Thanks

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Clarification of the MicroPython fork

Post by pythoncoder » Tue Feb 20, 2018 5:46 am

My above explanation lacked clarity. Paul (@pfalcon) has forked the MicroPython project and its library. His library requires his version of the firmware. Your error occurred because you were using his library with the official MicroPython firmware.

This fork occurred recently and, unfortunately, upip installs library code from Paul's fork. Consequently using upip to install any MicroPython library is hazardous if you are running official firmware.

The examples in my tutorial should work with Paul's fork if running with his firmware, although this is untested.

I support the official MicroPython firmware and library so my revised installation instructions reflect this.
Peter Hinch
Index to my micropython libraries.

mkarliner
Posts: 8
Joined: Fri Sep 09, 2016 10:59 am

Re: uasyncio deque problem

Post by mkarliner » Tue Feb 20, 2018 6:16 pm

Thanks Peter,
just ran into the same problem myself, while trying to see if Boris' fork
now runs uasyncio.

This is worse than the Western Schism

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: uasyncio deque problem

Post by pythoncoder » Wed Feb 21, 2018 6:23 am

Indeed, although @Damien has clarified the situation here. Unfortunately the code on Pypi is from Paul's library.

The Loboris fork will run usayncio with the exception of task cancellation and timeouts. These use a relatively recent feature of the official firmware sources which haven't yet been incorporated into his fork.
Peter Hinch
Index to my micropython libraries.

Damien
Site Admin
Posts: 647
Joined: Mon Dec 09, 2013 5:02 pm

Re: uasyncio deque problem

Post by Damien » Wed Feb 21, 2018 12:51 pm

The ucollections.deque type has now been merged into the main, official repository. So the latest version of uasyncio from PyPI (uasyncio v2.0) should now work with the pyboard and other supported boards. I've tested it on a PYBLITEv1.0 and an ESP32 and it seems to work without any issues.

If you are downloading firmware from https://micropython.org/download then please wait at least 12 hours for new builds to become available.

Post Reply