Running webserver and other code simultaneously

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
User avatar
pythoncoder
Posts: 3666
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Running webserver and other code simultaneously

Post by pythoncoder » Tue Jun 05, 2018 6:55 am

You can indeed precompile code with mpy-cross and copy the .mpy file into your filesystem. This works fine but is something I seldom use because all it saves on the target is the compilation phase. It doesn't save RAM at runtime. Its main use is where you have a module which is too large to compile on the target but not too large to run without freezing. I've used it then because the turn-round time is much faster than the compile-link-flash cycle for frozen bytecode.

It can also save time if you have a module you want to run as soon as possible after boot, but this is rather a niche situation.
Peter Hinch

mvincm
Posts: 6
Joined: Sun Sep 17, 2017 10:59 pm

Re: Running webserver and other code simultaneously

Post by mvincm » Tue Apr 02, 2019 8:51 am

@pythoncoder

I think there is some bug in uasyncio lib. More info here:

https://github.com/micropython/micropyt ... issues/335

Could you be so kind and take a look?

Best regards,
MvincM

simonmcc
Posts: 12
Joined: Fri May 11, 2018 5:21 pm

Re: Running webserver and other code simultaneously

Post by simonmcc » Tue Apr 02, 2019 10:39 am

I just removed False from that function call on line 60 in uasyncio/__init__.py, and it works for me now - this is the diff output:

Code: Select all

60c60
<         self.poller.unregister(sock, False)
---
>         self.poller.unregister(sock)

kevinkk525
Posts: 510
Joined: Sat Feb 03, 2018 7:02 pm

Re: Running webserver and other code simultaneously

Post by kevinkk525 » Tue Apr 02, 2019 10:54 am

If you have that "False" in there, you are probably using the uasyncio version of pfalcon which is incompatible with the mainline micropython firmware. Make sure to use the version of uasyncio published at github.com/micropython/micropython-lib
Kevin Köck
Micropython Smarthome Firmware: https://github.com/kevinkk525/pysmartnode

mvincm
Posts: 6
Joined: Sun Sep 17, 2017 10:59 pm

Re: Running webserver and other code simultaneously

Post by mvincm » Tue Apr 02, 2019 12:01 pm

I can confirm. In the main line of uasyncio libs, there is no "False" so the problem is still there...

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

Re: Running webserver and other code simultaneously

Post by pythoncoder » Tue Apr 02, 2019 4:31 pm

This issue is discussed here with means of ensuring the correct library is installed.

I use uasyncio daily and it does not throw this error. The uasyncio version must match the firmware.
Peter Hinch

User avatar
Roberthh
Posts: 1710
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Running webserver and other code simultaneously

Post by Roberthh » Tue Apr 02, 2019 7:02 pm

Peter, didn't you want to point at https://github.com/peterhinch/micropython-async

mvincm
Posts: 6
Joined: Sun Sep 17, 2017 10:59 pm

Re: Running webserver and other code simultaneously

Post by mvincm » Tue Apr 02, 2019 7:13 pm

Take a look here:

https://github.com/micropython/micropyt ... issues/335

Problem is gone! Thanks, everyone!

MvincM

mvincm
Posts: 6
Joined: Sun Sep 17, 2017 10:59 pm

Re: Running webserver and other code simultaneously

Post by mvincm » Wed Apr 03, 2019 8:57 pm

BTW...

I have read a tutorial for uasyncio but I can't understand main difference "call_soon vs create_task". When we should use which function?

Like in this example:

https://github.com/micropython/micropyt ... _server.py

Why we use call_soon instead create_task (both works well).

Could someone be so kind and explain to me?

Best regards,
MvincM

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

Re: Running webserver and other code simultaneously

Post by pythoncoder » Fri Apr 05, 2019 7:59 am

call_soon is intended for callback functions (i.e. synchronous code defined with def foo(args)). create_task is for coroutines defined with async def foo(args).

A point of contention with the asyncio paradigm is the distinction between synchronous and asynchronous functions: this has become known as "the red and blue function problem". I recommend you read this excellent article for background. Python's asyncio (and uasyncio) do maintain this distinction. In MicroPython one is a function (obviously) and the other is a generator.
Peter Hinch

Post Reply