diff options
author | Eric Snow <ericsnowcurrently@gmail.com> | 2023-10-17 23:05:49 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-17 23:05:49 (GMT) |
commit | c58c63fdf615a1c2bfc995dd0b938d82e32b6cde (patch) | |
tree | c55bd273d26fdcaf30013a44a7bf973940771a23 /Lib/test/support/interpreters.py | |
parent | 7029c1a1c5b864056aa00298b1d0e0269f073f99 (diff) | |
download | cpython-c58c63fdf615a1c2bfc995dd0b938d82e32b6cde.zip cpython-c58c63fdf615a1c2bfc995dd0b938d82e32b6cde.tar.gz cpython-c58c63fdf615a1c2bfc995dd0b938d82e32b6cde.tar.bz2 |
gh-84570: Add Timeouts to SendChannel.send() and RecvChannel.recv() (gh-110567)
Diffstat (limited to 'Lib/test/support/interpreters.py')
-rw-r--r-- | Lib/test/support/interpreters.py | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/Lib/test/support/interpreters.py b/Lib/test/support/interpreters.py index 9ba6862..f8f42c0 100644 --- a/Lib/test/support/interpreters.py +++ b/Lib/test/support/interpreters.py @@ -170,15 +170,25 @@ class RecvChannel(_ChannelEnd): _end = 'recv' - def recv(self, *, _sentinel=object(), _delay=10 / 1000): # 10 milliseconds + def recv(self, timeout=None, *, + _sentinel=object(), + _delay=10 / 1000, # 10 milliseconds + ): """Return the next object from the channel. This blocks until an object has been sent, if none have been sent already. """ + if timeout is not None: + timeout = int(timeout) + if timeout < 0: + raise ValueError(f'timeout value must be non-negative') + end = time.time() + timeout obj = _channels.recv(self._id, _sentinel) while obj is _sentinel: time.sleep(_delay) + if timeout is not None and time.time() >= end: + raise TimeoutError obj = _channels.recv(self._id, _sentinel) return obj @@ -203,12 +213,12 @@ class SendChannel(_ChannelEnd): _end = 'send' - def send(self, obj): + def send(self, obj, timeout=None): """Send the object (i.e. its data) to the channel's receiving end. This blocks until the object is received. """ - _channels.send(self._id, obj, blocking=True) + _channels.send(self._id, obj, timeout=timeout, blocking=True) def send_nowait(self, obj): """Send the object to the channel's receiving end. @@ -221,12 +231,12 @@ class SendChannel(_ChannelEnd): # See bpo-32604 and gh-19829. return _channels.send(self._id, obj, blocking=False) - def send_buffer(self, obj): + def send_buffer(self, obj, timeout=None): """Send the object's buffer to the channel's receiving end. This blocks until the object is received. """ - _channels.send_buffer(self._id, obj, blocking=True) + _channels.send_buffer(self._id, obj, timeout=timeout, blocking=True) def send_buffer_nowait(self, obj): """Send the object's buffer to the channel's receiving end. |