How to use non blocking functions, asynchronous tasks
-
- Posts: 3
- Joined: Tue Jan 10, 2017 3:10 pm
How to use non blocking functions, asynchronous tasks
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.
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.
Re: How to use non blocking functions, asynchronous tasks
You might be interested in this thread just next to yours: http://forum.micropython.org/viewtopic.php?f=2&t=2837
-
- Posts: 3
- Joined: Tue Jan 10, 2017 3:10 pm
Re: How to use non blocking functions, asynchronous tasks
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 :/
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 :/
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to use non blocking functions, asynchronous tasks
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.
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.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
-
- Posts: 3
- Joined: Tue Jan 10, 2017 3:10 pm
Re: How to use non blocking functions, asynchronous tasks
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'
>>>
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'
>>>
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to use non blocking functions, asynchronous tasks
The README in the link to the MicroPython library tells you how to install it.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to use non blocking functions, asynchronous tasks
@pythoncoder,
Perhaps I could use your experience and knowledge. How can I simultaneously run two loops using uasyncio, something similar to _thread?
Example with _thread:
import _thread as th
def loop1():
while True:
#do something
def loop2():
while True:
#do something else
th.start_new_thread(loop1, ())
th.start_new_thread(loop2, ())
This works fine on pyboard, however there is no support for _thread on ESP8266. Please note that in the project I'm working on, the second loop, loop2 is using socket to check whether there is a connection to the server running on ESP8266
Thanks for your help.
Perhaps I could use your experience and knowledge. How can I simultaneously run two loops using uasyncio, something similar to _thread?
Example with _thread:
import _thread as th
def loop1():
while True:
#do something
def loop2():
while True:
#do something else
th.start_new_thread(loop1, ())
th.start_new_thread(loop2, ())
This works fine on pyboard, however there is no support for _thread on ESP8266. Please note that in the project I'm working on, the second loop, loop2 is using socket to check whether there is a connection to the server running on ESP8266
Thanks for your help.
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: How to use non blocking functions, asynchronous tasks
I suggest you look at the unofficial uasyncio tutorial here. You'll find example code, including aledflash.py. This is a very simple Pyboard demo which runs four copies of a coroutine to flash the four onboard LED's asynchronously.
Peter Hinch
Index to my micropython libraries.
Index to my micropython libraries.
Re: How to use non blocking functions, asynchronous tasks
Thanks for your suggestion. I'm digging into it.
Re: How to use non blocking functions, asynchronous tasks
I have become familiar with cooperative multitasking using uasyncio library after digging into the example you posted for reference. However, I can't still achieve the desired goal. I'm using socket.socket object awaiting for a connection to happen and from what i read, its functions are blocking functions. Any suggestion will be appreciated