Skip to content

Switch await core.sleep to await core.io_queue.queue_[read|write] #33

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions asyncio/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ async def read(self, n):
"""

core._io_queue.queue_read(self.s)
await core.sleep(0)
yield
return self.s.read(n)

async def readinto(self, buf):
Expand All @@ -73,7 +73,7 @@ async def readinto(self, buf):
"""

core._io_queue.queue_read(self.s)
await core.sleep(0)
yield
return self.s.readinto(buf)

async def readexactly(self, n):
Expand All @@ -88,7 +88,7 @@ async def readexactly(self, n):
r = b""
while n:
core._io_queue.queue_read(self.s)
await core.sleep(0)
yield
r2 = self.s.read(n)
if r2 is not None:
if not len(r2):
Expand All @@ -106,7 +106,7 @@ async def readline(self):
l = b""
while True:
core._io_queue.queue_read(self.s)
await core.sleep(0)
yield
l2 = self.s.readline() # may do multiple reads but won't block
l += l2
if not l2 or l[-1] == 10: # \n (check l in case l2 is str)
Expand All @@ -129,7 +129,8 @@ async def drain(self):
mv = memoryview(self.out_buf)
off = 0
while off < len(mv):
yield core._io_queue.queue_write(self.s)
core._io_queue.queue_write(self.s)
yield
ret = self.s.write(mv[off:])
if ret is not None:
off += ret
Expand Down
2 changes: 2 additions & 0 deletions examples/usb_cdc_boot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import usb_cdc
usb_cdc.enable(data=True, console=True)
34 changes: 34 additions & 0 deletions examples/usb_cdc_example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
example that reads from the cdc data serial port in groups of four and prints
to the console. The USB CDC data serial port will need enabling. This can be done
by copying examples/usb_cdc_boot.py to boot.py in the CIRCUITPY directory

Meanwhile a simple counter counts up every second and also prints
to the console.
"""


import asyncio
import usb_cdc

async def client():
usb_cdc.data.timeout=0
s = asyncio.StreamReader(usb_cdc.data)
while True:
text = await s.readline()
print(text)
await asyncio.sleep(0)

async def counter():
i = 0
while True:
print(i)
i += 1
await asyncio.sleep(1)

async def main():
client_task = asyncio.create_task(client())
count_task = asyncio.create_task(counter())
await asyncio.gather(client_task, count_task)

asyncio.run(main())