summaryrefslogtreecommitdiffstatshomepage
path: root/tests/extmod/uasyncio_new_event_loop.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2020-04-09 13:15:47 +1000
committerDamien George <damien.p.george@gmail.com>2020-04-13 22:16:52 +1000
commitdb137e70dcf67de26828e17c2d3dc9d21e971eb0 (patch)
treeb6e5e8cd0b2f63ed169379a33c3021c156008148 /tests/extmod/uasyncio_new_event_loop.py
parent1bbc15dd15b232f833b6cc2066bd9ca2b7fe6b66 (diff)
downloadmicropython-db137e70dcf67de26828e17c2d3dc9d21e971eb0.tar.gz
micropython-db137e70dcf67de26828e17c2d3dc9d21e971eb0.zip
extmod/uasyncio: Add Loop.new_event_loop method.
This commit adds Loop.new_event_loop() which is used to reset the singleton event loop. This functionality is put here instead of in Loop.close() to make it possible to write code that is compatible with CPython.
Diffstat (limited to 'tests/extmod/uasyncio_new_event_loop.py')
-rw-r--r--tests/extmod/uasyncio_new_event_loop.py36
1 files changed, 36 insertions, 0 deletions
diff --git a/tests/extmod/uasyncio_new_event_loop.py b/tests/extmod/uasyncio_new_event_loop.py
new file mode 100644
index 0000000000..b711befba9
--- /dev/null
+++ b/tests/extmod/uasyncio_new_event_loop.py
@@ -0,0 +1,36 @@
+# Test Loop.new_event_loop()
+
+try:
+ import uasyncio as asyncio
+except ImportError:
+ try:
+ import asyncio
+ except ImportError:
+ print("SKIP")
+ raise SystemExit
+
+
+async def task():
+ for i in range(4):
+ print("task", i)
+ await asyncio.sleep(0)
+ await asyncio.sleep(0)
+
+
+async def main():
+ print("start")
+ loop.create_task(task())
+ await asyncio.sleep(0)
+ print("stop")
+ loop.stop()
+
+
+# Use default event loop to run some tasks
+loop = asyncio.get_event_loop()
+loop.create_task(main())
+loop.run_forever()
+
+# Create new event loop, old one should not keep running
+loop = asyncio.new_event_loop()
+loop.create_task(main())
+loop.run_forever()