Asyncio task creation and cancellation problem #16253
-
Trying to get two blinking LEDs to start: (corrected task1 and task2) async def test():
while True:
if push_button is True:
task1 = asyncio.create_task(blink_red(house_red))
task2 = asyncio.create_task(blink_green(gate_green))
asyncio.run(test()) sometimes didn't work. So, I thought I would try to check for proper # SuperFastPython.com
# example of canceling a scheduled task
import asyncio
# define a coroutine for a task
async def task_coroutine():
# report a message
print('executing the task')
# block for a moment
await asyncio.sleep(1)
# custom coroutine
async def main():
# report a message
print('main coroutine started')
# create and schedule the task
task = asyncio.create_task(task_coroutine())
# cancel the task before it has had a chance to run
was_cancelled = task.cancel()
# report whether the cancel request was successful
print(f'was canceled: {was_cancelled}')
# wait a moment
await asyncio.sleep(0.1)
# check the status of the task
print(f'canceled: {task.cancelled()}')
# report a final message
print('main coroutine done')
# start the asyncio program
asyncio.run(main()) Running this program throws-up this error:
Do you need to test that the task was really created, in the first place? I want a system that is totally reliable. P.S. while loops need |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
The task class in Python has the cancelled method but in micropython it does not, and has a much smaller set of methods. Have a look by adding print(dir(task)) to the Superfast code snippet and then run it in normal python and then run the code again in micropython, but deleting the print(f'canceled: {task.cancelled()}') line which will cause micropython to hiccup. You can then see the python / micropython differences for async tasks In your own code snippet I think the likes of task1 = asyncio.run(blink_red(house_red)) looks wrong. I'm no expert, but I think you should only run one asyncio.run loop (i.e the async def test() function in your snippet). I've only created and run tasks with mytask = asyncio.create_task(somthing()) type of syntax. |
Beta Was this translation helpful? Give feedback.
-
@beetlegigg is correct with
In typical applications the call to |
Beta Was this translation helpful? Give feedback.
The task class in Python has the cancelled method but in micropython it does not, and has a much smaller set of methods. Have a look by adding print(dir(task)) to the Superfast code snippet and then run it in normal python and then run the code again in micropython, but deleting the print(f'canceled: {task.cancelled()}') line which will cause micropython to hiccup. You can then see the python / micropython differences for async tasks
In your own code snippet I think the likes of task1 = asyncio.run(blink_red(house_red)) looks wrong. I'm no expert, but I think you should only run one asyncio.run loop (i.e the async def test() function in your snippet). I've only created and run tasks with …