Skip to content

Commit 1c1379e

Browse files
Add a timeout to RecvChannel.recv().
1 parent c49820e commit 1c1379e

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

Lib/test/support/interpreters.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,25 @@ class RecvChannel(_ChannelEnd):
170170

171171
_end = 'recv'
172172

173-
def recv(self, *, _sentinel=object(), _delay=10 / 1000): # 10 milliseconds
173+
def recv(self, timeout=None, *,
174+
_sentinel=object(),
175+
_delay=10 / 1000, # 10 milliseconds
176+
):
174177
"""Return the next object from the channel.
175178
176179
This blocks until an object has been sent, if none have been
177180
sent already.
178181
"""
182+
if timeout is not None:
183+
timeout = int(timeout)
184+
if timeout < 0:
185+
raise ValueError(f'timeout value must be non-negative')
186+
end = time.now() + timeout
179187
obj = _channels.recv(self._id, _sentinel)
180188
while obj is _sentinel:
181189
time.sleep(_delay)
190+
if timeout is not None and time.now() >= end:
191+
raise TimeoutError
182192
obj = _channels.recv(self._id, _sentinel)
183193
return obj
184194

0 commit comments

Comments
 (0)