asyncio forking a task

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

asyncio forking a task

Post by devnull » Mon Jun 18, 2018 1:46 am

I have googled and searched the forums but can't find an answer.

Very new to asyncio, but is it possible to fork a task as you can with a thread ?

If so, does anybody have a code snippet ?

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

Re: asyncio forking a task

Post by pythoncoder » Mon Jun 18, 2018 8:14 am

I'm not entirely sure what you mean. It's possible to launch multiple instances of a task: in this example four instances of the toggle task are launched (with different args).
Peter Hinch
Index to my micropython libraries.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: asyncio forking a task

Post by devnull » Tue Jun 19, 2018 2:19 am

Hi Peter;

What I mean is to launch a task and let it run on it's own and then be able to continue to use the repl as you can with threads.

Kind of fire and forget ?!

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

Re: asyncio forking a task

Post by pythoncoder » Tue Jun 19, 2018 5:48 am

This query seems to come up regularly; I'm puzzled why people want to run a background task on a microcontroller. I'd be interested to hear about applications for this.

But, to answer your question, (u)asyncio doesn't work like that. When you run

Code: Select all

loop.run_forever()
or

Code: Select all

loop.run_until_complete(my_coro())
uasyncio runs and monopolises the MicroPython VM. There is nothing magic about uasyncio: it's just Python code and it takes over from the REPL until complete. As Python code I'd recommend to those interested to take a look and see how it works. It's clever but not impenetrable ;)

I see no reason why you shouldn't run a uasyncio application as a thread, but I haven't tried this.
Peter Hinch
Index to my micropython libraries.

Post Reply