Page 1 of 1

uasyncio deque problem

Posted: Sun Feb 18, 2018 5:35 pm
by Jim.S
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())

Re: uasyncio deque problem

Posted: Mon Feb 19, 2018 7:11 am
by pythoncoder
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.

Re: uasyncio deque problem

Posted: Mon Feb 19, 2018 9:39 pm
by Jim.S
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

Clarification of the MicroPython fork

Posted: Tue Feb 20, 2018 5:46 am
by pythoncoder
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.

Re: uasyncio deque problem

Posted: Tue Feb 20, 2018 6:16 pm
by mkarliner
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

Re: uasyncio deque problem

Posted: Wed Feb 21, 2018 6:23 am
by pythoncoder
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.

Re: uasyncio deque problem

Posted: Wed Feb 21, 2018 12:51 pm
by Damien
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.