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.
pull/188/head
Paul Sokolovsky 2017-06-07 03:06:12 +03:00
rodzic 87e30182a1
commit 75e1474ddf
1 zmienionych plików z 5 dodań i 4 usunięć

Wyświetl plik

@ -131,17 +131,18 @@ class StreamWriter:
self.s = s
self.extra = extra
def awrite(self, buf):
def awrite(self, buf, off=0, sz=-1):
# This method is called awrite (async write) to not proliferate
# incompatibility with original asyncio. Unlike original asyncio
# whose .write() method is both not a coroutine and guaranteed
# to return immediately (which means it has to buffer all the
# data), this method is a coroutine.
sz = len(buf)
if sz == -1:
sz = len(buf) - off
if DEBUG and __debug__:
log.debug("StreamWriter.awrite(): spooling %d bytes", sz)
while True:
res = self.s.write(buf)
res = self.s.write(buf, off, sz)
# If we spooled everything, return immediately
if res == sz:
if DEBUG and __debug__:
@ -152,7 +153,7 @@ class StreamWriter:
if DEBUG and __debug__:
log.debug("StreamWriter.awrite(): spooled partial %d bytes", res)
assert res < sz
buf = buf[res:]
off += res
sz -= res
yield IOWrite(self.s)
#assert s2.fileno() == self.s.fileno()