Page 1 of 1

PicoWeb Shared Processing

Posted: Fri Nov 17, 2017 4:46 am
by bitninja
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!

Re: PicoWeb Shared Processing

Posted: Fri Nov 17, 2017 11:09 am
by pythoncoder
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).

Re: PicoWeb Shared Processing

Posted: Fri Nov 17, 2017 5:35 pm
by bitninja
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.

Re: PicoWeb Shared Processing

Posted: Fri Nov 17, 2017 6:49 pm
by pythoncoder
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.

Re: PicoWeb Shared Processing

Posted: Fri Nov 17, 2017 7:01 pm
by bitninja
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...

Re: PicoWeb Shared Processing

Posted: Sat Nov 18, 2017 9:35 am
by pythoncoder
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.

Re: PicoWeb Shared Processing

Posted: Mon Nov 20, 2017 6:57 am
by pfalcon
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.