Skip to content

Commit 7580e7b

Browse files
committed
StreamReader.readinto: document, strip debug and repeated comments.
1 parent e3787f0 commit 7580e7b

File tree

3 files changed

+40
-17
lines changed

3 files changed

+40
-17
lines changed

FASTPOLL.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ This version has the following features relative to official V2.0:
3030
* `run_until_complete(coro())` now returns the value returned by `coro()` as
3131
per CPython
3232
[micropython-lib PR270](https://github.com/micropython/micropython-lib/pull/270).
33+
* The `StreamReader` class now has a `readinto(buf, n=0)` method to enable
34+
allocations to be reduced.
3335

3436
Note that priority device drivers are written by using the officially supported
3537
technique for writing stream I/O drivers. Code using such drivers will run
@@ -64,6 +66,9 @@ formerly provided by `asyncio_priority.py` is now implemented.
6466
3.1 [Fast IO](./FASTPOLL.md#31-fast-io)
6567
3.2 [Low Priority](./FASTPOLL.md#32-low-priority)
6668
3.3 [Other Features](./FASTPOLL.md#33-other-features)
69+
3.3.1 [Version](./FASTPOLL.md#331-version)
70+
3.3.2 [got_event_loop](./FASTPOLL.md#332-got_event_loop)
71+
3.3.3 [StreamReader readinto method](./FASTPOLL.md#333-streamreader-readinto-method)
6772
3.4 [Low priority yield](./FASTPOLL.md#34-low-priority-yield)
6873
3.4.1 [Task Cancellation and Timeouts](./FASTPOLL.md#341-task-cancellation-and-timeouts)
6974
3.5 [Low priority callbacks](./FASTPOLL.md#35-low-priority-callbacks)
@@ -347,10 +352,14 @@ See [Low priority callbacks](./FASTPOLL.md#35-low-priority-callbacks)
347352

348353
## 3.3 Other Features
349354

355+
### 3.3.1 Version
356+
350357
Variable:
351-
* `version` Returns a 2-tuple. Current contents ('fast_io', '0.24'). Enables
358+
* `version` Returns a 2-tuple. Current contents ('fast_io', '0.25'). Enables
352359
the presence and realease state of this version to be determined at runtime.
353360

361+
### 3.3.2 got_event_loop
362+
354363
Function:
355364
* `got_event_loop()` No arg. Returns a `bool`: `True` if the event loop has
356365
been instantiated. Enables code using the event loop to raise an exception if
@@ -374,6 +383,31 @@ loop = asyncio.get_event_loop(runq_len=40, waitq_len=40)
374383
This is mainly for retro-fitting to existing classes and functions. The
375384
preferred approach is to pass the event loop to classes as a constructor arg.
376385

386+
### 3.3.3 StreamReader readinto method
387+
388+
The purpose of this asynchronous method is to be a non-allocating complement to
389+
the `StreamReader.read` method, enabling data to be read into a pre-existing
390+
buffer. It assumes that the device driver providing the data has a `readinto`
391+
method.
392+
393+
`StreamReader.readinto(buf, n=0)` args:
394+
`buf` the buffer to read into.
395+
`n=0` the maximum number of bytes to read - default the buffer size.
396+
available it will be placed in the buffer. The return value is the number of
397+
bytes read. The default maximum is the buffer size, otherwise the value of `n`.
398+
399+
The method will pause (allowing other coros to run) until data is available.
400+
401+
This method calls the synchronous `readinto` method of the data source. This
402+
may take one arg (the buffer) or two (the buffer followed by the maximum number
403+
of bytes to read). If `StreamReader.readinto` is launched with a single arg,
404+
the `readinto` method will receive that one arg.
405+
406+
It is the reponsibility of the device `readinto` method to validate the args,
407+
to populate the buffer and to return the number of bytes read. It should return
408+
"immediately" with as much data as is available. It will only be called when
409+
the `ioctl` method indicates that read data is ready.
410+
377411
###### [Contents](./FASTPOLL.md#contents)
378412

379413
## 3.4 Low priority yield

fast_io/__init__.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,29 +164,18 @@ def read(self, n=-1):
164164
# PollEventLoop._unregister
165165
return res # Next iteration raises StopIteration and returns result
166166

167-
def readinto(self, buf, n=0): # Experimental and not yet tested TODO
168-
if DEBUG and __debug__:
169-
log.debug("StreamReader.readinto() START")
170-
167+
def readinto(self, buf, n=0): # See comments in .read
171168
while True:
172169
yield IORead(self.polls)
173-
if DEBUG and __debug__:
174-
log.debug("StreamReader.readinto() ... just after IORead")
175170
if n:
176-
res = self.ios.readinto(buf, n) # Call the device's readinto method
171+
res = self.ios.readinto(buf, n) # Call device's readinto method
177172
else:
178173
res = self.ios.readinto(buf)
179174
if res is not None:
180175
break
181-
# This should not happen for real sockets, but can easily
182-
# happen for stream wrappers (ssl, websockets, etc.)
183176
#log.warn("Empty read")
184-
yield IOReadDone(self.polls) # uasyncio.core calls remove_reader
185-
if DEBUG and __debug__:
186-
log.debug("StreamReader.readinto() ... just after IOReadDone")
187-
# This de-registers device as a read device with poll via
188-
# PollEventLoop._unregister
189-
return res # Next iteration raises StopIteration and returns result
177+
yield IOReadDone(self.polls)
178+
return res
190179

191180
def readexactly(self, n):
192181
buf = b""

fast_io/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# Code at https://github.com/peterhinch/micropython-async.git
99
# fork: peterhinch/micropython-lib branch: uasyncio-io-fast-and-rw
1010

11-
version = ('fast_io', '0.24')
11+
version = ('fast_io', '0.25')
1212
try:
1313
import rtc_time as time # Low power timebase using RTC
1414
except ImportError:

0 commit comments

Comments
 (0)