uasyncio - How detect the end task in another task.

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
prem111
Posts: 63
Joined: Sun Feb 23, 2020 3:18 pm

uasyncio - How detect the end task in another task.

Post by prem111 » Wed Jun 17, 2020 6:28 pm

How to detect the end or „is running” of a task that is running in another task. How to detect termination or running task: some_function.run() ?

Code: Select all

async def service1():

    while True:
    ...
    ap_task = uasyncio.create_task(some_function.run())

    await uasyncio.sleep(0)

async def main():

    tasks = (service1, service2)
        res = await uasyncio.funcs.gather(*tasks, return_exceptions=False)

uasyncio.run(main())

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

Re: uasyncio - How detect the end task in another task.

Post by kevinkk525 » Wed Jun 17, 2020 6:30 pm

You currently can't because it is not implemented in the Task class.

But you can gather multiple tasks, that would show their termination.
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

prem111
Posts: 63
Joined: Sun Feb 23, 2020 3:18 pm

Re: uasyncio - How detect the end task in another task.

Post by prem111 » Wed Jun 17, 2020 6:36 pm

How to do it ?

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

Re: uasyncio - How detect the end task in another task.

Post by kevinkk525 » Wed Jun 17, 2020 8:57 pm

it works just like in CPython but you can take a look here: https://github.com/peterhinch/micropyth ... UTORIAL.md
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

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

Re: uasyncio - How detect the end task in another task.

Post by pythoncoder » Thu Jun 18, 2020 5:13 am

prem111 wrote:
Wed Jun 17, 2020 6:28 pm

Code: Select all

        [code]res = await uasyncio.funcs.gather(*tasks, return_exceptions=False)
This line should read

Code: Select all

res = await uasyncio.gather(*tasks, return_exceptions=False)
Gather is described here.

I'm not entirely sure what you're trying to achieve, though. Consider the following:

Code: Select all

import uasyncio as asyncio

async def foo():
   for _ in range(5):
       # do something
       await asyncio.sleep_ms(100)  # slow coro

task = asyncio.create_task(foo())
async def bar():
   await asyncio.sleep(1)
   # You can await task if you want to pause until it's run
   # Or test its status as follows
   try:
       await asyncio.wait_for_ms(task, 1)
   except asyncio.TimeoutError:
       print('foo is running')
   else:
      print('foo has stopped')

asyncio.run(bar())
It's perhaps not ideal, but it works here on a Pyboard. See for reference
Peter Hinch

Post Reply