Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async repl #106

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Async repl #106

wants to merge 3 commits into from

Conversation

wom-bat
Copy link
Contributor

@wom-bat wom-bat commented Aug 28, 2024

This is a straw-man proof of concept. It allows interacting with a REPL for examples/webserver on the qemu_virt_aarch64 platform

I can create a file 'extra.py' on the NFS server, that contains:

from microdot.microdot import URLPattern, Response

def add_action(app, url, action):
    p = URLPattern(url)
    app.url_map = [(['GET'], p, action)] + app.url_map

counter = 0

def foo(request):
    global counter
    head = '''
    <html>
      <body>
        <h1>Foo</h1>
    '''
    tail = '''
      </body>
    </html>
'''
    counter = counter + 1
    body = head + str(counter) + tail
    headers = {'Content-Type': 'text/html'}
    return Response(body=body, headers = headers)

Then on the commandline do:

--> import sys
--> sys.path.append('micropython')
--> from extra import foo, add_action
--> add_action(app, '/foo', foo)

Then visiting the webserver's /foo prints out the counter value.

It'd be trivial to extend to deleting the path and modifying existing ones.

The Asyncio repl is a bit limited compared with the standard one: the biggest limitation is one can use only single line commands.

If this is considered a good idea, I'll add it to the other webserver example platforms.

to allow control-C to interrupt current task in async repl.

Signed-off-by: Peter Chubb <[email protected]>
This allows interaction via the serial port, and on-the-fly update of
the webserver's routing rules.

For example one can do:
```
import asyncio
from microdot.microdot import URLPattern, Response

def add_action(app, url, action):
    p = URLPattern(url)
    app.url_map = [(['GET'], p, action)] + app.url_map
```
to add new rules for the main webserver app.

Signed-off-by: Peter Chubb <[email protected]>
@Ivan-Velickovic
Copy link
Collaborator

Seems like a good idea to me, given that it's completely optional.

The main limitation I see is that it needs the script that you are running to allow use aiorepl, but I guess that's a limitation with MicroPython itself and not this PR.

I'll play around with the PR and see how well it works.

@Ivan-Velickovic
Copy link
Collaborator

One question I have is, is there anyway to stop a script that's running if you're in the async REPL?

@wom-bat
Copy link
Contributor Author

wom-bat commented Aug 28, 2024

One question I have is, is there anyway to stop a script that's running if you're in the async REPL?
Yes there is.

From the async repl, it feels (almost) exactly the same as a standard repl, except that other things are running in the background.

You can either set it up to share the initial context (as I did) or to have its own. If it has the initial context, then you can do:

app.shutdown()

to stop the webserver.

If you start new coroutines from the repl, you can use all asyncio's methods to control them

Copy link
Contributor

@JE-Archer JE-Archer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems useful to me with no downside. I pushed odroidc4 support.

@Ivan-Velickovic
Copy link
Collaborator

Okay, is there any reason why this is a draft?

@Ivan-Velickovic
Copy link
Collaborator

We need to start documenting what sub-set of MicroPython features we support. This being one of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants