WebREPL and uasyncio

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
boognevatz
Posts: 3
Joined: Sun Jul 03, 2022 11:37 am

WebREPL and uasyncio

Post by boognevatz » Tue Aug 02, 2022 12:20 pm

Hi,

If I run a uasyncio task, while the task is running, the WebREPL prompt seems totally frozen.

Here is a demo code (tested on esp32):

boot.py:

Code: Select all

import network
ap = network.WLAN(network.AP_IF) # create access-point interface
ap.config(essid='ESPTEST')
ap.config(authmode=3, password='testitbaby') # set the SSID of the access point
ap.active(True)

import webrepl
webrepl.start()
main.py:

Code: Select all

import uasyncio
import time

class Context:
    WATCHDOG_TIMEOUT = 3
    watchdog_last_feed_epoch = 0

def webrepl_active():
    prev = uos.dupterm(None)
    uos.dupterm(prev)
    if prev:
        return True
    return False

async def periodical_tasks(context):
    while True:
        print('current time is:', time.time())
        print('watchdog_last_feed_epoch:', context.watchdog_last_feed_epoch)

        if webrepl_active():
            context.watchdog_last_feed_epoch = time.time()
            print('webrepl is active!')

        if time.time() - context.watchdog_last_feed_epoch > context.WATCHDOG_TIMEOUT:
            print ('prepare_for_deepsleep')

        gc.collect()
        await uasyncio.sleep(context.WATCHDOG_TIMEOUT)

async def main(context):
    uasyncio.create_task(periodical_tasks(context))
    await uasyncio.sleep(20)

context = Context()

# uasyncio.run(main(context))

Connect to the board via webrepl from a browser, then issue the command uasyncio.run(main(context))

While the async task is running (20 sec), I can not type anything into the command line.
However I can upload files, and disconnect and (re)connect to the board, and I can even enter the password.
But the command line is frozen.

My goal is to go to deepsleep via a background service after 20 sec inactivity (button input or lack of webrepl connection). And wakeup after 10 minutes or button input (irq). And be *able* to use webrepl.

The board is on a battery, and it runs out of juice after about 4 hours without deepsleep. For developing, I just want to leave it on, and connect it whenever necessary.

Any idea how to launch webrepl so it stays responsible, while there is an uasyncio background task scheduled/running?

p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

Re: WebREPL and uasyncio

Post by p_j » Thu Aug 04, 2022 8:15 am

I don't fully understand the mechanics of it but you are correct, you can't use the REPL/WEBREPL while a uasyncio event loop is running.

For your case I would use a software WDT to put the chip in deepsleep

https://github.com/peterhinch/micropyth ... oft_wdt.py

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

Re: WebREPL and uasyncio

Post by pythoncoder » Thu Aug 04, 2022 8:25 am

In Python (Cpython and MicroPython) while a program is running the REPL is inactive. You can interrupt execution with ctrl-C but any other typing is ignored. In this respect a uasyncio program is no different from any other.
Peter Hinch
Index to my micropython libraries.

p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

Re: WebREPL and uasyncio

Post by p_j » Thu Aug 04, 2022 8:33 am

pythoncoder wrote:
Thu Aug 04, 2022 8:25 am
In Python (Cpython and MicroPython) while a program is running the REPL is inactive. You can interrupt execution with ctrl-C but any other typing is ignored. In this respect a uasyncio program is no different from any other.
Hi Peter,

Are you able to expand on this a little bit please, there are some libraries such as ftp that will run happily in the background while the REPL is active. I assume this is because they are in a thread? Could the uasyncio event loop be run in a thread allowing access to the repl?

Thanks

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

Re: WebREPL and uasyncio

Post by pythoncoder » Thu Aug 04, 2022 8:39 am

There are a few specialist applications which can run in the background but they are very much the exception. Anything you write will not unless you've mastered some tricky programming techniques. Simply using uasyncio does not achieve background running.
Peter Hinch
Index to my micropython libraries.

p_j
Posts: 102
Joined: Mon Aug 23, 2021 1:08 pm
Location: Sydney

Re: WebREPL and uasyncio

Post by p_j » Thu Aug 04, 2022 8:41 am

pythoncoder wrote:
Thu Aug 04, 2022 8:39 am
There are a few specialist applications which can run in the background but they are very much the exception. Anything you write will not unless you've mastered some tricky programming techniques. Simply using uasyncio does not achieve background running.
Thanks Peter

boognevatz
Posts: 3
Joined: Sun Jul 03, 2022 11:37 am

Re: WebREPL and uasyncio

Post by boognevatz » Fri Aug 05, 2022 3:35 pm

pythoncoder wrote:
Thu Aug 04, 2022 8:39 am
There are a few specialist applications which can run in the background but they are very much the exception. Anything you write will not unless you've mastered some tricky programming techniques. Simply using uasyncio does not achieve background running.
Thank you for the detailed reply!
Can you please explain a little, why the webrepl login works, and why the file uploading works? Only the interactive shell is frozen.
Why those are able to run in the background?

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

Re: WebREPL and uasyncio

Post by pythoncoder » Mon Aug 08, 2022 8:02 am

Long ago I read an explanation but I've forgotten the details. I suggest you raise a separate query or study the sourcecode.
Peter Hinch
Index to my micropython libraries.

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: WebREPL and uasyncio

Post by Christian Walther » Mon Aug 08, 2022 9:08 am

There is an undocumented socket option that lets you register a callback that will then, if I understand correctly, be invoked “in between” regular Python execution whenever there is data/connections on the socket. Implementation, use.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: WebREPL and uasyncio

Post by jimmo » Thu Aug 11, 2022 1:34 am

boognevatz wrote:
Fri Aug 05, 2022 3:35 pm
Can you please explain a little, why the webrepl login works, and why the file uploading works? Only the interactive shell is frozen.
Why those are able to run in the background?
The important thing to remember is that webrepl is mirrored to the same REPL as the device (there is only a single REPL). So it's not that asyncio is freezing webrepl or anything, it's just that you have code running and so your device is not currently running the REPL prompt.

However, if your code was doing print() (or input()) then you would see that via the webrepl.

The reason login and file transfer works is the same reason as you described for an FTP server -- webrepl is running in the background (via the mechanism that Christian described).

Post Reply