Skip to content

Commit f704ac5

Browse files
author
Paul Sokolovsky
committed
uasyncio: StreamReader: Separate "poll socket" vs "I/O socket".
Poll socket is what's passed to uselect.poll(), while I/O socket is what's used for .read(). This is a workaround of the issue that MicroPython doesn't support proxying poll functionality for stream wrappers (like SSL, websocket, etc.) This issue is tracked as micropython/micropython#3394 It may be that it's more efficient to apply such a workaround on uasyncio level rather than implementing full solution of uPy side.
1 parent bd82808 commit f704ac5

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

uasyncio/uasyncio/__init__.py

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -90,28 +90,33 @@ def wait(self, delay):
9090

9191
class StreamReader:
9292

93-
def __init__(self, s):
94-
self.s = s
93+
def __init__(self, polls, ios=None):
94+
if ios is None:
95+
ios = polls
96+
self.polls = polls
97+
self.ios = ios
9598

9699
def read(self, n=-1):
97100
while True:
98-
yield IORead(self.s)
99-
res = self.s.read(n)
101+
yield IORead(self.polls)
102+
res = self.ios.read(n)
100103
if res is not None:
101104
break
102-
log.warn("Empty read")
105+
# This should not happen for real sockets, but can easily
106+
# happen for stream wrappers (ssl, websockets, etc.)
107+
#log.warn("Empty read")
103108
if not res:
104-
yield IOReadDone(self.s)
109+
yield IOReadDone(self.polls)
105110
return res
106111

107112
def readexactly(self, n):
108113
buf = b""
109114
while n:
110-
yield IORead(self.s)
111-
res = self.s.read(n)
115+
yield IORead(self.polls)
116+
res = self.ios.read(n)
112117
assert res is not None
113118
if not res:
114-
yield IOReadDone(self.s)
119+
yield IOReadDone(self.polls)
115120
break
116121
buf += res
117122
n -= len(res)
@@ -122,11 +127,11 @@ def readline(self):
122127
log.debug("StreamReader.readline()")
123128
buf = b""
124129
while True:
125-
yield IORead(self.s)
126-
res = self.s.readline()
130+
yield IORead(self.polls)
131+
res = self.ios.readline()
127132
assert res is not None
128133
if not res:
129-
yield IOReadDone(self.s)
134+
yield IOReadDone(self.polls)
130135
break
131136
buf += res
132137
if buf[-1] == 0x0a:
@@ -136,11 +141,11 @@ def readline(self):
136141
return buf
137142

138143
def aclose(self):
139-
yield IOReadDone(self.s)
140-
self.s.close()
144+
yield IOReadDone(self.polls)
145+
self.ios.close()
141146

142147
def __repr__(self):
143-
return "<StreamReader %r>" % self.s
148+
return "<StreamReader %r %r>" % (self.polls, self.ios)
144149

145150

146151
class StreamWriter:

0 commit comments

Comments
 (0)