uasyncio : missing RuntimeError: This event loop is already running
I've just spent far too long debugging, what is essentially the following code:
#!/usr/bin/env python
try:
import asyncio
except ImportError:
import uasyncio as asyncio
loop = asyncio.get_event_loop()
async def foo():
print('foo()')
async def main():
loop.run_until_complete(foo())
while True:
print('app_loop()')
await asyncio.sleep(1)
if __name__ == '__main__':
loop.run_until_complete(main())
I know what you're thinking.... why run_until_complete inside an async function.
I completely agree, and now that I have the root cause, it's easy to fix.
However: this was nested inside a relatively large code-base, and I had no trace-back leading me to the root cause.
Finding this required a lot of cutting code until the problem went away, which wasn't helped by it being in 2 places.
Run on CPython
When run on Python 3.9.9, it raises.
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/usr/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
return future.result()
File "<stdin>", line 2, in main
File "/usr/lib/python3.9/asyncio/base_events.py", line 623, in run_until_complete
self._check_running()
File "/usr/lib/python3.9/asyncio/base_events.py", line 583, in _check_running
raise RuntimeError('This event loop is already running')
RuntimeError: This event loop is already running
Run on micropython
When the above code is executed in micropython I get the following output.
MicroPython v1.17 on 2021-09-02; PYBD-SF6W with STM32F767IIK
Type "help()" for more information.
>>>
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== # ... above code pasted here
===
foo()
app_loop()
>>>
Execution took 1 second, no exception raised.
Unix build: uasyncio __await__ not called
Is this special method supported? The following code behaves differently under CPython and MicroPython.
import uasyncio as asyncio
class Foo():
def __iter__(self):
for n in range(5):
print('__iter__ called')
yield
def __await__(self):
for n in range(5):
print('__await__ called')
yield
async def bar():
foo = Foo()
print('waiting for foo')
await foo
print('done')
async def main(delay):
await asyncio.sleep(delay)
print("I've seen starships burn off the shoulder of Orion...")
print("Time to die...")
loop = asyncio.get_event_loop()
loop.create_task(bar())
loop.run_until_complete(main(5))
Under CPython the outcome is as expected: __iter__() is not called, __await__() is:
Python 3.5.2 (default, Dec 2 2016, 15:29:25)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import rats6
waiting for foo
__await__ called
5
4
3
2
1
done
I've seen starships burn off the shoulder of Orion...
Time to die...
>>>
Under MicroPython the converse is true:
MicroPython v1.8.1-925-g46e59c5 on 2016-12-03; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
>>> import rats6
waiting for foo
__iter__ called
__iter__ called
__iter__ called
__iter__ called
__iter__ called
done
I've seen starships burn off the shoulder of Orion...
Time to die...
>>>