Skip to content

Commit 0c36d81

Browse files
authored
Merge pull request #33 from furbrain/main
Switch `await core.sleep` to `await core.io_queue.queue_[read|write]`
2 parents 745f1df + a82c50a commit 0c36d81

File tree

4 files changed

+103
-14
lines changed

4 files changed

+103
-14
lines changed

asyncio/core.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,15 @@ def _dequeue(self, s):
148148
del self.map[id(s)]
149149
self.poller.unregister(s)
150150

151-
def queue_read(self, s):
151+
async def queue_read(self, s):
152152
self._enqueue(s, 0)
153+
_never.state = False
154+
await _never
153155

154-
def queue_write(self, s):
156+
async def queue_write(self, s):
155157
self._enqueue(s, 1)
158+
_never.state = False
159+
await _never
156160

157161
def remove(self, task):
158162
while True:

asyncio/stream.py

+7-12
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ async def read(self, n):
6060
This is a coroutine.
6161
"""
6262

63-
core._io_queue.queue_read(self.s)
64-
await core.sleep(0)
63+
await core._io_queue.queue_read(self.s)
6564
return self.s.read(n)
6665

6766
async def readinto(self, buf):
@@ -72,8 +71,7 @@ async def readinto(self, buf):
7271
This is a coroutine, and a MicroPython extension.
7372
"""
7473

75-
core._io_queue.queue_read(self.s)
76-
await core.sleep(0)
74+
await core._io_queue.queue_read(self.s)
7775
return self.s.readinto(buf)
7876

7977
async def readexactly(self, n):
@@ -87,8 +85,7 @@ async def readexactly(self, n):
8785

8886
r = b""
8987
while n:
90-
core._io_queue.queue_read(self.s)
91-
await core.sleep(0)
88+
await core._io_queue.queue_read(self.s)
9289
r2 = self.s.read(n)
9390
if r2 is not None:
9491
if not len(r2):
@@ -105,8 +102,7 @@ async def readline(self):
105102

106103
l = b""
107104
while True:
108-
core._io_queue.queue_read(self.s)
109-
await core.sleep(0)
105+
await core._io_queue.queue_read(self.s)
110106
l2 = self.s.readline() # may do multiple reads but won't block
111107
l += l2
112108
if not l2 or l[-1] == 10: # \n (check l in case l2 is str)
@@ -129,7 +125,7 @@ async def drain(self):
129125
mv = memoryview(self.out_buf)
130126
off = 0
131127
while off < len(mv):
132-
yield core._io_queue.queue_write(self.s)
128+
await core._io_queue.queue_write(self.s)
133129
ret = self.s.write(mv[off:])
134130
if ret is not None:
135131
off += ret
@@ -166,8 +162,7 @@ async def open_connection(host, port):
166162
except OSError as er:
167163
if er.errno != EINPROGRESS:
168164
raise er
169-
core._io_queue.queue_write(s)
170-
await core.sleep(0)
165+
await core._io_queue.queue_write(s)
171166
return ss, ss
172167

173168

@@ -201,7 +196,7 @@ async def _serve(self, s, cb):
201196
# Accept incoming connections
202197
while True:
203198
try:
204-
yield core._io_queue.queue_read(s)
199+
await core._io_queue.queue_read(s)
205200
except core.CancelledError:
206201
# Shutdown server
207202
s.close()

examples/serial_examples.py

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# SPDX-FileCopyrightText: 2022 Phil Underwood
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
example that reads from the cdc data serial port in groups of four and prints
6+
to the console. The USB CDC data serial port will need enabling. This can be done
7+
by copying examples/usb_cdc_boot.py to boot.py in the CIRCUITPY directory
8+
9+
Meanwhile a simple counter counts up every second and also prints
10+
to the console.
11+
"""
12+
13+
14+
import asyncio
15+
16+
USE_USB = True
17+
USE_UART = True
18+
USE_BLE = True
19+
20+
21+
if USE_USB:
22+
import usb_cdc
23+
24+
async def usb_client():
25+
26+
usb_cdc.data.timeout = 0
27+
s = asyncio.StreamReader(usb_cdc.data)
28+
while True:
29+
text = await s.readline()
30+
print("USB: ", text)
31+
32+
33+
if USE_UART:
34+
import board
35+
36+
async def uart_client():
37+
38+
uart = board.UART()
39+
uart.timeout = 0
40+
s = asyncio.StreamReader(board.UART())
41+
while True:
42+
text = await s.readexactly(4)
43+
print("UART: ", text)
44+
45+
46+
if USE_BLE:
47+
from adafruit_ble import BLERadio
48+
from adafruit_ble.advertising.standard import ProvideServicesAdvertisement
49+
from adafruit_ble.services.nordic import UARTService
50+
51+
async def ble_client():
52+
ble = BLERadio()
53+
uart = UARTService()
54+
advertisement = ProvideServicesAdvertisement(uart)
55+
ble.start_advertising(advertisement)
56+
s = asyncio.StreamReader(uart._rx) # pylint: disable=protected-access
57+
while True:
58+
text = await s.read(6)
59+
print("BLE: ", text)
60+
61+
62+
async def counter():
63+
i = 0
64+
while True:
65+
print(i)
66+
i += 1
67+
await asyncio.sleep(1)
68+
69+
70+
async def main():
71+
clients = [asyncio.create_task(counter())]
72+
if USE_USB:
73+
clients.append(asyncio.create_task(usb_client()))
74+
if USE_UART:
75+
clients.append(asyncio.create_task(uart_client()))
76+
if USE_BLE:
77+
clients.append(asyncio.create_task(ble_client()))
78+
await asyncio.gather(*clients)
79+
80+
81+
asyncio.run(main())

examples/usb_cdc_boot.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-FileCopyrightText: 2022 Phil Underwood
2+
#
3+
# SPDX-License-Identifier: Unlicense
4+
"""
5+
Save this file as boot.py on CIRCUITPY to enable the usb_cdc.data serial device
6+
"""
7+
import usb_cdc
8+
9+
usb_cdc.enable(data=True, console=True)

0 commit comments

Comments
 (0)