PicoWeb Shared Processing

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
bitninja
Posts: 145
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

PicoWeb Shared Processing

Post by bitninja » Fri Nov 17, 2017 4:46 am

I am using PicoWeb, and I had a question about simultaneously running a small DNS service, to redirect any web requests to the device itself aka a captive portal. I have a smaller working script that handles DNS and HTTP requests in the same "main" loop, but with PicoWeb I do not know how to get my DNS code into the main loop of the app.run().

Can any Python guru explain the "natural" way this would be done?

Thanks!

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

Re: PicoWeb Shared Processing

Post by pythoncoder » Fri Nov 17, 2017 11:09 am

You need to do something along these lines

Code: Select all

async def my_dns_server(args):
    while True:
        # code

# In the picoweb run() routine
        loop.create_task(asyncio.start_server(self._handle, host, port))
        loop.create_task(my_dns_server(args))
        loop.run_forever()
I assume some familiarity with uasyncio here.

There is an unofficial tutorial on the MicroPython asyncio subset here https://github.com/peterhinch/micropython-async.git (follow the link to tutorial).
Peter Hinch

bitninja
Posts: 145
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

Re: PicoWeb Shared Processing

Post by bitninja » Fri Nov 17, 2017 5:35 pm

Thank you Peter!

I'll have to learn to usage of asyncio so I can understand what it is doing, but I get the jist of what I need to do. My only question is, am I pretty much required to make modifications to PicoWeb itself to accomplish this? I'm ok with this... I already have a fork that I keep for other needs... I just wanted to ask.

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

Re: PicoWeb Shared Processing

Post by pythoncoder » Fri Nov 17, 2017 6:49 pm

I see no need to modify PicoWeb as the two tasks are essentially independent (unless I'm missing something in what you're trying to do). Whether the ESP8266 has enough RAM to accomplish both tasks concurrently is another matter.
Peter Hinch

bitninja
Posts: 145
Joined: Thu Sep 15, 2016 4:09 pm
Location: Spring, Texas

Re: PicoWeb Shared Processing

Post by bitninja » Fri Nov 17, 2017 7:01 pm

When you say
# In the picoweb run() routine
I thought you meant to modify this...

https://github.com/pfalcon/picoweb/blob ... __.py#L266

Sorry for being dense...

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

Re: PicoWeb Shared Processing

Post by pythoncoder » Sat Nov 18, 2017 9:35 am

OK, we're talking at cross purposes. I did indeed mean you to add a single line to the run() routine, and you'd also need a line to import the module containing your code. What I meant was that no changes would be needed to the PicoWeb logic.

But on reflection there is a better and cleaner way which involves no change to PicoWeb. Your own module could have

Code: Select all

import picoweb
import uasyncio as asyncio

async def my_dns_server(args):
    while True:
        # code

def run():
    loop = asyncio.get_event_loop()
    loop.create_task(my_dns_server(args))
    picoweb.run(picoweb_args)
I haven't tested this, but in principle I think it should work.
Peter Hinch

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: PicoWeb Shared Processing

Post by pfalcon » Mon Nov 20, 2017 6:57 am

I've added example of this to the picoweb repo: https://github.com/pfalcon/picoweb/blob ... ush.py#L89 . But there's no magic in it - you just get the current loop and .create_task() in it, then when picoweb server is started with WebApp.run(), your task will run too.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply