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
what's wrong ?
thanks !
JP