Skip to content

Commit 6ad3001

Browse files
committed
Switch await core.sleep to yield
1 parent fbdb77d commit 6ad3001

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

asyncio/stream.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ async def read(self, n):
6161
"""
6262

6363
core._io_queue.queue_read(self.s)
64-
await core.sleep(0)
64+
yield
6565
return self.s.read(n)
6666

6767
async def readinto(self, buf):
@@ -73,7 +73,7 @@ async def readinto(self, buf):
7373
"""
7474

7575
core._io_queue.queue_read(self.s)
76-
await core.sleep(0)
76+
yield
7777
return self.s.readinto(buf)
7878

7979
async def readexactly(self, n):
@@ -88,7 +88,7 @@ async def readexactly(self, n):
8888
r = b""
8989
while n:
9090
core._io_queue.queue_read(self.s)
91-
await core.sleep(0)
91+
yield
9292
r2 = self.s.read(n)
9393
if r2 is not None:
9494
if not len(r2):
@@ -106,7 +106,7 @@ async def readline(self):
106106
l = b""
107107
while True:
108108
core._io_queue.queue_read(self.s)
109-
await core.sleep(0)
109+
yield
110110
l2 = self.s.readline() # may do multiple reads but won't block
111111
l += l2
112112
if not l2 or l[-1] == 10: # \n (check l in case l2 is str)
@@ -129,7 +129,8 @@ async def drain(self):
129129
mv = memoryview(self.out_buf)
130130
off = 0
131131
while off < len(mv):
132-
yield core._io_queue.queue_write(self.s)
132+
core._io_queue.queue_write(self.s)
133+
yield
133134
ret = self.s.write(mv[off:])
134135
if ret is not None:
135136
off += ret

examples/usb_cdc_boot.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import usb_cdc
2+
usb_cdc.enable(data=True, console=True)

examples/usb_cdc_example.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
"""
2+
example that reads from the cdc data serial port in groups of four and prints
3+
to the console. The USB CDC data serial port will need enabling. This can be done
4+
by copying examples/usb_cdc_boot.py to boot.py in the CIRCUITPY directory
5+
6+
Meanwhile a simple counter counts up every second and also prints
7+
to the console.
8+
"""
9+
10+
11+
import asyncio
12+
import usb_cdc
13+
14+
async def client():
15+
usb_cdc.data.timeout=0
16+
s = asyncio.StreamReader(usb_cdc.data)
17+
while True:
18+
text = await s.readline()
19+
print(text)
20+
await asyncio.sleep(0)
21+
22+
async def counter():
23+
i = 0
24+
while True:
25+
print(i)
26+
i += 1
27+
await asyncio.sleep(1)
28+
29+
async def main():
30+
client_task = asyncio.create_task(client())
31+
count_task = asyncio.create_task(counter())
32+
await asyncio.gather(client_task, count_task)
33+
34+
asyncio.run(main())

0 commit comments

Comments
 (0)