Skip to content

Commit 75e1474

Browse files
author
Paul Sokolovsky
committed
uasyncio: awrite: Use 3-arg .write(), accept offset/size too.
Use MicroPython .write() extension of passing offset/size to efficiently spool buffers larger than socket output buffer. Also, make awrite() accept these params too.
1 parent 87e3018 commit 75e1474

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

uasyncio/uasyncio/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -131,17 +131,18 @@ def __init__(self, s, extra):
131131
self.s = s
132132
self.extra = extra
133133

134-
def awrite(self, buf):
134+
def awrite(self, buf, off=0, sz=-1):
135135
# This method is called awrite (async write) to not proliferate
136136
# incompatibility with original asyncio. Unlike original asyncio
137137
# whose .write() method is both not a coroutine and guaranteed
138138
# to return immediately (which means it has to buffer all the
139139
# data), this method is a coroutine.
140-
sz = len(buf)
140+
if sz == -1:
141+
sz = len(buf) - off
141142
if DEBUG and __debug__:
142143
log.debug("StreamWriter.awrite(): spooling %d bytes", sz)
143144
while True:
144-
res = self.s.write(buf)
145+
res = self.s.write(buf, off, sz)
145146
# If we spooled everything, return immediately
146147
if res == sz:
147148
if DEBUG and __debug__:
@@ -152,7 +153,7 @@ def awrite(self, buf):
152153
if DEBUG and __debug__:
153154
log.debug("StreamWriter.awrite(): spooled partial %d bytes", res)
154155
assert res < sz
155-
buf = buf[res:]
156+
off += res
156157
sz -= res
157158
yield IOWrite(self.s)
158159
#assert s2.fileno() == self.s.fileno()

0 commit comments

Comments
 (0)