asyncio : cancelling gathered tasks

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
jpbihin
Posts: 7
Joined: Thu Feb 08, 2018 5:31 pm
Location: Belgium

asyncio : cancelling gathered tasks

Post by jpbihin » Sat Oct 24, 2020 5:14 pm

As an exercise, I set myself this goal : cancelling gathered tasks.
I'm working with asyncio V3 and a ESP32

Code: Select all

from machine import Pin
import uasyncio as asyncio
from pushbutton import Pushbutton
# pushbutton is Kevin Köck's module

p = Pin(26, Pin.IN, Pin.PULL_UP)
pb = Pushbutton(p)

async def fn_1() :
    while True :
        print("fn_1 every 3 s") 
        print(time.time())
        await asyncio.sleep(4)

async def fn_2() :
    while True :
        print("fn_2 every 6s")
        print(time.time())
        await asyncio.sleep(6)

async def foo():
    tasks = (fn_1(), fn_2())
    await asyncio.gather(*tasks)
    print("in foo")

async def cancel_foo():
    print("ready to cancel")
    foo_task.cancel()
    await asyncio.sleep(0)
    print ("game over")
    
async def main() :
    global foo_task
    pb.press_func(cancel_foo)
    foo_task = asyncio.create_task(foo())
    while True :
        await asyncio.sleep(1) 
        print(".")
 

try : 
    asyncio.run(main())
except Exception as e:
    print(e)
    sys.exit()
finally:
    asyncio.new_event_loop()  # Clear retained state

the result when I push the button : fn_1 is cancelled, not fn_2
what's wrong ?
thanks !
JP

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

Re: asyncio : cancelling gathered tasks

Post by kevinkk525 » Sat Oct 24, 2020 5:55 pm

That's a bug that hasn't been fixed yet: https://github.com/micropython/micropython/issues/5798
I'm hoping that Damien will revisit uasyncio soon..
Kevin Köck
Micropython Smarthome Firmware (with Home-Assistant integration): https://github.com/kevinkk525/pysmartnode

jpbihin
Posts: 7
Joined: Thu Feb 08, 2018 5:31 pm
Location: Belgium

Re: asyncio : cancelling gathered tasks

Post by jpbihin » Sat Oct 24, 2020 6:32 pm

Thanks Kevin !
in the meantime this is working fine...

Code: Select all

from machine import Pin
import uasyncio as asyncio
from pushbutton import Pushbutton

p = Pin(26, Pin.IN, Pin.PULL_UP)
pb = Pushbutton(p)

async def fn_1() :
    print("fn_1 every 3 s") 
    print(time.time())
    await asyncio.sleep(4)

async def fn_2() :
    print("fn_2 every 6s")
    print(time.time())
    await asyncio.sleep(6)

async def foo_1():
    while True:
        await fn_1()
        print("in foo_1")

async def foo_2():
    while True:
        await fn_2()
        print("in foo_2")
        

async def cancel_foo():
    print("ready to cancel")
    foo_task_1.cancel()
    await asyncio.sleep(0)
    foo_task_2.cancel()
    await asyncio.sleep(0)
    print ("game over")
    
async def main() :
    global foo_task_1, foo_task_2
    pb.press_func(cancel_foo)
    foo_task_1 = asyncio.create_task(foo_1())
    foo_task_2 = asyncio.create_task(foo_2())
    while True :
        await asyncio.sleep(1) 
        print(".")
 

try : 
    asyncio.run(main())
except Exception as e:
    print(e)
    sys.exit()
finally:
    asyncio.new_event_loop()  # Clear retained state


Post Reply