kopia lustrzana https://github.com/micropython/micropython-lib
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
rodzic
87e30182a1
commit
75e1474ddf
|
@ -131,17 +131,18 @@ class StreamWriter:
|
||||||
self.s = s
|
self.s = s
|
||||||
self.extra = extra
|
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
|
# This method is called awrite (async write) to not proliferate
|
||||||
# incompatibility with original asyncio. Unlike original asyncio
|
# incompatibility with original asyncio. Unlike original asyncio
|
||||||
# whose .write() method is both not a coroutine and guaranteed
|
# whose .write() method is both not a coroutine and guaranteed
|
||||||
# to return immediately (which means it has to buffer all the
|
# to return immediately (which means it has to buffer all the
|
||||||
# data), this method is a coroutine.
|
# data), this method is a coroutine.
|
||||||
sz = len(buf)
|
if sz == -1:
|
||||||
|
sz = len(buf) - off
|
||||||
if DEBUG and __debug__:
|
if DEBUG and __debug__:
|
||||||
log.debug("StreamWriter.awrite(): spooling %d bytes", sz)
|
log.debug("StreamWriter.awrite(): spooling %d bytes", sz)
|
||||||
while True:
|
while True:
|
||||||
res = self.s.write(buf)
|
res = self.s.write(buf, off, sz)
|
||||||
# If we spooled everything, return immediately
|
# If we spooled everything, return immediately
|
||||||
if res == sz:
|
if res == sz:
|
||||||
if DEBUG and __debug__:
|
if DEBUG and __debug__:
|
||||||
|
@ -152,7 +153,7 @@ class StreamWriter:
|
||||||
if DEBUG and __debug__:
|
if DEBUG and __debug__:
|
||||||
log.debug("StreamWriter.awrite(): spooled partial %d bytes", res)
|
log.debug("StreamWriter.awrite(): spooled partial %d bytes", res)
|
||||||
assert res < sz
|
assert res < sz
|
||||||
buf = buf[res:]
|
off += res
|
||||||
sz -= res
|
sz -= res
|
||||||
yield IOWrite(self.s)
|
yield IOWrite(self.s)
|
||||||
#assert s2.fileno() == self.s.fileno()
|
#assert s2.fileno() == self.s.fileno()
|
||||||
|
|
Ładowanie…
Reference in New Issue