How to use non blocking functions, asynchronous tasks

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
dexterlabora
Posts: 3
Joined: Tue Jan 10, 2017 3:10 pm

How to use non blocking functions, asynchronous tasks

Postby dexterlabora » Tue Jan 10, 2017 3:25 pm

What's the easiest way to run a function as a non blocking task?

I'm used to writing in JavaScript where you can easily have long or infinite tasks running in the background or that can be triggered by event handlers.

```
setTimeout( function(){
console.log("my super long task");
}, 10000);
console.log("end of file");

results:
end of file
my super long task
```


Goal:
Run a simple web server to accept a command (this is an infinite loop, waiting on clients to connect).
Each command starts an infinite loop for an LED display routine (there are multiple LED displays).
I have several routines that can run simultaneously.

User avatar
deshipu
Posts: 770
Joined: Thu May 28, 2015 5:54 pm

Re: How to use non blocking functions, asynchronous tasks

Postby deshipu » Tue Jan 10, 2017 9:34 pm

You might be interested in this thread just next to yours: viewtopic.php?f=2&t=2837

dexterlabora
Posts: 3
Joined: Tue Jan 10, 2017 3:10 pm

Re: How to use non blocking functions, asynchronous tasks

Postby dexterlabora » Wed Jan 11, 2017 10:17 am

Thanks for the feedback. I looked at this, but it appears I have to install a third party library on my ESP8266 (WeMos D1), but the instructions were a little vague.

I also found this blog post, which uses the "threading" library. This sounds exactly like what I need, but it doesn't appear to be available on the ESP8266 version of micropython either.
https://www.pycom.io/qa-micropython-mul ... collector/

I thought this first project was just going to be a "Hello World" to run an infinite LED display loop, but still allow an HTTP interface to change the pattern. Its now become a journey to do this :/

User avatar
pythoncoder
Posts: 1241
Joined: Fri Jul 18, 2014 8:01 am

Re: How to use non blocking functions, asynchronous tasks

Postby pythoncoder » Wed Jan 11, 2017 4:13 pm

The uasyncio library is not a third party library: it's part of the official MicroPython library https://github.com/micropython/micropython-lib. Further asyncio is the standard Python way to achieve cooperative multi-tasking - uasyncio is a subset designed for MicroPython on microcontrollers. It has some enhancements to facilitate millisecond-level scheduling.

The _thread library supports pre-emptive multi tasking and is much more resource intensive. As I understand it the ESP8266 doesn't support it.

On a device as constrained as the ESP8266 uasyncio is the way to proceed.

dexterlabora
Posts: 3
Joined: Tue Jan 10, 2017 3:10 pm

Re: How to use non blocking functions, asynchronous tasks

Postby dexterlabora » Wed Jan 11, 2017 4:46 pm

Thanks for the detailed response, very appreciated.

Sadly, when I try to import this library, its not available.

MicroPython v1.8.7-7-gb5a1a20a3 on 2017-01-09; ESP module with ESP8266
Type "help()" for more information.
>>> import uasyncio
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: no module named 'uasyncio'
>>>

User avatar
pythoncoder
Posts: 1241
Joined: Fri Jul 18, 2014 8:01 am

Re: How to use non blocking functions, asynchronous tasks

Postby pythoncoder » Thu Jan 12, 2017 11:25 am

The README in the link to the MicroPython library tells you how to install it.


Return to “General Discussion and Questions”

Who is online

Users browsing this forum: No registered users and 5 guests