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!
PicoWeb Shared Processing
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: PicoWeb Shared Processing
You need to do something along these lines
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).
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()
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
Index to my micropython libraries.
Index to my micropython libraries.
Re: PicoWeb Shared Processing
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.
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.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: PicoWeb Shared Processing
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
Index to my micropython libraries.
Index to my micropython libraries.
Re: PicoWeb Shared Processing
When you say
https://github.com/pfalcon/picoweb/blob ... __.py#L266
Sorry for being dense...
I thought you meant to modify this...# In the picoweb run() routine
https://github.com/pfalcon/picoweb/blob ... __.py#L266
Sorry for being dense...
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: PicoWeb Shared Processing
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
I haven't tested this, but in principle I think it should work.
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)
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: PicoWeb Shared Processing
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/
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/