Change uasyncio instantiated class during run time

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
UpStream
Posts: 2
Joined: Sat Sep 05, 2020 1:31 pm

Change uasyncio instantiated class during run time

Post by UpStream » Fri Oct 22, 2021 12:22 pm

Hi all! This is my first post here. I'm new to micropython.

So here is my history:
I'm writing my own neopixel version, using webserver polling method to change the neopixel pattern.
So needed to change the behave of a coro during run time.
I did not find any code examples so far, so I create my own and that works!
The problem is; ESP8266 hangs after a long while running perfectly, say after one hour or more.
It hangs by itself, during running alone, no webserver access.
Exception messages is not suppressed.
No error message printed, it just stop blinking and answering the web requests.
I'm able to use ctrl+c on thonny.

My question is:
Is it the right way to change a running coro?
Or am I doing it wrong?

Code: Select all

# A global variable ( mode ) holds the name of the class with contains different patter each one.
# It is modified by webserver request handler every time the user change it on web page.
# ( this_mode ) is the default mode after reboot.

mode = Stars

async def blink(this_mode, period_ms):
    m = this_mode()
    while True:
        m.run()
        if this_mode != mode:
            this_mode = mode
            m = mode()
            gc.collect()
        await uasyncio.sleep_ms(period_ms)
        
def main():
    wlan = do_connect()
    myip = wlan.ifconfig()[0]

    socket_listen()

    event_loop = uasyncio.get_event_loop()
    event_loop.create_task(serve_requests(myip, handler, 50)) # 50
    event_loop.create_task(blink(mode, 50))
    event_loop.run_forever()

    gc.collect()

main()

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

Re: Change uasyncio instantiated class during run time

Post by pythoncoder » Fri Oct 22, 2021 1:13 pm

I can't see anything wrong with your code - checking a global in this way should be fine. I can also confirm that an ESP8266 can run uasyncio networking applications for much longer than an hour (though not, in my experience, indefinitely).

In uasyncio V3 it is possible to lose the event_loop stuff - see my tutorial - but this is just a simplification and won't fix the problem. Have you any interrupt based code that might be causing problems?
Peter Hinch
Index to my micropython libraries.

User avatar
UpStream
Posts: 2
Joined: Sat Sep 05, 2020 1:31 pm

Re: Change uasyncio instantiated class during run time

Post by UpStream » Mon Oct 25, 2021 1:14 pm

Hi pythoncoder !
I have nothing else than the server and blink coros running, no interruptions is in use.
And I think I found the problem on a concurrent access that happens is specific situation when leaving the web page opened long time, as it call the server periodically to check status. It is much more steady now. I also made modifications on server code to fail gracefully when the web page is closed before the web server finished sending it.

I also would to say I'm enjoying a lot to program using micropython.
The amount of code and support published by the community is very nice too.
I was able to get from an idea to a project running in less than two months in my spare time.


Thanks you all!

Post Reply