running async code from interrupt? #15478
Answered
by
bixb922
ThinkTransit
asked this question in
ESP32
-
Is it possible to run an async function from a pin interrupt? For example, not sure why this doesn't work, any ideas? async def go_to_sleep():
# Custom code that requires async
machine.deepsleep(60*1000)
def sleep_callback(p):
asyncio.create_task(go_to_sleep)
# asyncio.run(go_to_sleep)
p0 = Pin(14, Pin.IN)
p0.irq(trigger=Pin.IRQ_FALLING, handler=sleep_callback) Traceback (most recent call last):
File "main.py", line 35, in sleep_callback
File "asyncio/core.py", line 1, in create_task
TypeError: coroutine expected |
Beta Was this translation helpful? Give feedback.
Answered by
bixb922
Jul 17, 2024
Replies: 1 comment 1 reply
-
The correct way to create a task is However, it's not a good idea to launch a task from a ISR (interrupt service routine) . Please see this excellent tutorial https://github.com/peterhinch/micropython-async/blob/master/v3/docs/INTERRUPTS.md One good way to do this is to leave an asyncio task running always awaiting a ThreadSafeFlag that is set by the ISR. |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
ThinkTransit
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The correct way to create a task is
asyncio.create_task(go_to_sleep() )
, with ()However, it's not a good idea to launch a task from a ISR (interrupt service routine) . Please see this excellent tutorial https://github.com/peterhinch/micropython-async/blob/master/v3/docs/INTERRUPTS.md
One good way to do this is to leave an asyncio task running always awaiting a ThreadSafeFlag that is set by the ISR.