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 ?
asyncio forking a task
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: asyncio forking a task
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.
Index to my micropython libraries.
Re: asyncio forking a task
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 ?!
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 ?!
- pythoncoder
- Posts: 5956
- Joined: Fri Jul 18, 2014 8:01 am
- Location: UK
- Contact:
Re: asyncio forking a task
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
or
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.
But, to answer your question, (u)asyncio doesn't work like that. When you run
Code: Select all
loop.run_forever()
Code: Select all
loop.run_until_complete(my_coro())
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.
Index to my micropython libraries.