Can't use asyncio.sleep in gather? #13043
-
I tried to add a sleep() to a gather() but MicroPython v1.21 gives me an error "coroutine expected"? Am I doing it wrong? Simplified example: import asyncio
async def main():
await asyncio.gather(asyncio.sleep(5))
print("done")
asyncio.run(main())
Thanks in advance |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
shouldn't that be a 1-item tuple (or a list): import asyncio
async def main():
await asyncio.gather((asyncio.sleep(5),))
print("done")
asyncio.run(main()) since gather wants an iterable? |
Beta Was this translation helpful? Give feedback.
-
@skyguy For efficiency, asyncio.sleep is a bit "magic" and isn't implemented as a true coroutine. You'll need to wrap it in a task to make this work. (It's worth it though, this optimisation means you can do |
Beta Was this translation helpful? Give feedback.
-
Oops, my apology. I should have re-checked.
Marcus
… On Nov 22, 2023, at 4:51 AM, Peter Hinch ***@***.***> wrote:
To avoid confusion gather does not want an iterable. It takes an arbitrary number of positional args each of which is an awaitable. If aws is a sequence of awaitable objects the call signature is
awaitable asyncio.gather(*aws, return_exceptions=False) # aws is unpacked
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you commented.Message ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
@skyguy For efficiency, asyncio.sleep is a bit "magic" and isn't implemented as a true coroutine. You'll need to wrap it in a task to make this work.
(It's worth it though, this optimisation means you can do
await asyncio.sleep
without causing an allocation)