summaryrefslogtreecommitdiff
path: root/extmod/uasyncio/stream.py
diff options
context:
space:
mode:
Diffstat (limited to 'extmod/uasyncio/stream.py')
-rw-r--r--extmod/uasyncio/stream.py10
1 files changed, 10 insertions, 0 deletions
diff --git a/extmod/uasyncio/stream.py b/extmod/uasyncio/stream.py
index af3b8feab..750d8e974 100644
--- a/extmod/uasyncio/stream.py
+++ b/extmod/uasyncio/stream.py
@@ -56,9 +56,19 @@ class Stream:
return l
def write(self, buf):
+ if not self.out_buf:
+ # Try to write immediately to the underlying stream.
+ ret = self.s.write(buf)
+ if ret == len(buf):
+ return
+ if ret is not None:
+ buf = buf[ret:]
self.out_buf += buf
async def drain(self):
+ if not self.out_buf:
+ # Drain must always yield, so a tight loop of write+drain can't block the scheduler.
+ return await core.sleep_ms(0)
mv = memoryview(self.out_buf)
off = 0
while off < len(mv):