Skip to content

Commit db137e7

Browse files
committed
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.
1 parent 1bbc15d commit db137e7

4 files changed

Lines changed: 62 additions & 7 deletions

File tree

docs/library/uasyncio.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,13 @@ Event Loop
244244

245245
Return the event loop used to schedule and run tasks. See `Loop`.
246246

247+
.. function:: new_event_loop()
248+
249+
Reset the event loop and return it.
250+
251+
Note: since MicroPython only has a single event loop this function just
252+
resets the loop's state, it does not create a new one.
253+
247254
.. class:: Loop()
248255

249256
This represents the object which schedules and runs tasks. It cannot be

extmod/uasyncio/core.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,6 @@ def wait_io_event(self, dt):
132132
################################################################################
133133
# Main run loop
134134

135-
# TaskQueue of Task instances
136-
_task_queue = TaskQueue()
137-
138-
# Task queue and poller for stream IO
139-
_io_queue = IOQueue()
140-
141-
142135
# Ensure the awaitable is a task
143136
def _promote_to_task(aw):
144137
return aw if isinstance(aw, Task) else create_task(aw)
@@ -269,3 +262,16 @@ def call_exception_handler(context):
269262
# The runq_len and waitq_len arguments are for legacy uasyncio compatibility
270263
def get_event_loop(runq_len=0, waitq_len=0):
271264
return Loop
265+
266+
267+
def new_event_loop():
268+
global _task_queue, _io_queue
269+
# TaskQueue of Task instances
270+
_task_queue = TaskQueue()
271+
# Task queue and poller for stream IO
272+
_io_queue = IOQueue()
273+
return Loop
274+
275+
276+
# Initialise default event loop
277+
new_event_loop()
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Test Loop.new_event_loop()
2+
3+
try:
4+
import uasyncio as asyncio
5+
except ImportError:
6+
try:
7+
import asyncio
8+
except ImportError:
9+
print("SKIP")
10+
raise SystemExit
11+
12+
13+
async def task():
14+
for i in range(4):
15+
print("task", i)
16+
await asyncio.sleep(0)
17+
await asyncio.sleep(0)
18+
19+
20+
async def main():
21+
print("start")
22+
loop.create_task(task())
23+
await asyncio.sleep(0)
24+
print("stop")
25+
loop.stop()
26+
27+
28+
# Use default event loop to run some tasks
29+
loop = asyncio.get_event_loop()
30+
loop.create_task(main())
31+
loop.run_forever()
32+
33+
# Create new event loop, old one should not keep running
34+
loop = asyncio.new_event_loop()
35+
loop.create_task(main())
36+
loop.run_forever()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
start
2+
task 0
3+
stop
4+
start
5+
task 0
6+
stop

0 commit comments

Comments
 (0)