Skip to content

Commit 65605e3

Browse files
peterhinchpfalcon
authored andcommitted
uasyncio: Add test showing I/O scheduling starvation.
If there is a coroutine to run immediately (with wait delay <= 0), uasyncio.core never calls .wait() method, which is required to process I/O events (and schedule coroutines waiting for them). This test demonstrates the problem.
1 parent c7b277f commit 65605e3

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

uasyncio/test_io_starve.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
try:
2+
import uasyncio as asyncio
3+
except:
4+
import asyncio
5+
6+
try:
7+
import utime as time
8+
except:
9+
import time
10+
11+
done = False
12+
13+
async def receiver():
14+
global done
15+
with open('test_io_starve.py', 'rb') as f:
16+
sreader = asyncio.StreamReader(f)
17+
while True:
18+
await asyncio.sleep(0.1)
19+
res = await sreader.readline()
20+
# Didn't get there with the original problem this test shows
21+
done = True
22+
23+
24+
async def foo():
25+
start = time.time()
26+
while time.time() - start < 1:
27+
await asyncio.sleep(0)
28+
loop.stop()
29+
30+
loop = asyncio.get_event_loop()
31+
loop.create_task(foo())
32+
loop.create_task(receiver())
33+
loop.run_forever()
34+
assert done
35+
print('OK')

0 commit comments

Comments
 (0)