| 1 |
"""Cross-interpreter Channels High Level Module.""" |
| 2 |
|
| 3 |
import time |
| 4 |
import _interpchannels as _channels |
| 5 |
from concurrent.interpreters import _crossinterp |
| 6 |
|
| 7 |
# aliases: |
| 8 |
from _interpchannels import ( |
| 9 |
ChannelError, ChannelNotFoundError, ChannelClosedError, |
| 10 |
ChannelEmptyError, ChannelNotEmptyError, |
| 11 |
) |
| 12 |
from concurrent.interpreters._crossinterp import ( |
| 13 |
UNBOUND_ERROR, UNBOUND_REMOVE, |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
__all__ = [ |
| 18 |
'UNBOUND', 'UNBOUND_ERROR', 'UNBOUND_REMOVE', |
| 19 |
'create', 'list_all', |
| 20 |
'SendChannel', 'RecvChannel', |
| 21 |
'ChannelError', 'ChannelNotFoundError', 'ChannelEmptyError', |
| 22 |
'ItemInterpreterDestroyed', |
| 23 |
] |
| 24 |
|
| 25 |
|
| 26 |
class ItemInterpreterDestroyed(ChannelError, |
| 27 |
_crossinterp.ItemInterpreterDestroyed): |
| 28 |
"""Raised from get() and get_nowait().""" |
| 29 |
|
| 30 |
|
| 31 |
UNBOUND = _crossinterp.UnboundItem.singleton('queue', __name__) |
| 32 |
|
| 33 |
|
| 34 |
def _serialize_unbound(unbound): |
| 35 |
if unbound is UNBOUND: |
| 36 |
unbound = _crossinterp.UNBOUND |
| 37 |
return _crossinterp.serialize_unbound(unbound) |
| 38 |
|
| 39 |
|
| 40 |
def _resolve_unbound(flag): |
| 41 |
resolved = _crossinterp.resolve_unbound(flag, ItemInterpreterDestroyed) |
| 42 |
if resolved is _crossinterp.UNBOUND: |
| 43 |
resolved = UNBOUND |
| 44 |
return resolved |
| 45 |
|
| 46 |
|
| 47 |
def create(*, unbounditems=UNBOUND): |
| 48 |
"""Return (recv, send) for a new cross-interpreter channel. |
| 49 |
|
| 50 |
The channel may be used to pass data safely between interpreters. |
| 51 |
|
| 52 |
"unbounditems" sets the default for the send end of the channel. |
| 53 |
See SendChannel.send() for supported values. The default value |
| 54 |
is UNBOUND, which replaces the unbound item when received. |
| 55 |
""" |
| 56 |
unbound = _serialize_unbound(unbounditems) |
| 57 |
unboundop, = unbound |
| 58 |
cid = _channels.create(unboundop, -1) |
| 59 |
recv, send = RecvChannel(cid), SendChannel(cid) |
| 60 |
send._set_unbound(unboundop, unbounditems) |
| 61 |
return recv, send |
| 62 |
|
| 63 |
|
| 64 |
def list_all(): |
| 65 |
"""Return a list of (recv, send) for all open channels.""" |
| 66 |
channels = [] |
| 67 |
for cid, unboundop, _ in _channels.list_all(): |
| 68 |
chan = _, send = RecvChannel(cid), SendChannel(cid) |
| 69 |
if not hasattr(send, '_unboundop'): |
| 70 |
send._set_unbound(unboundop) |
| 71 |
else: |
| 72 |
assert send._unbound[0] == unboundop |
| 73 |
channels.append(chan) |
| 74 |
return channels |
| 75 |
|
| 76 |
|
| 77 |
class _ChannelEnd: |
| 78 |
"""The base class for RecvChannel and SendChannel.""" |
| 79 |
|
| 80 |
_end = None |
| 81 |
|
| 82 |
def __new__(cls, cid): |
| 83 |
self = super().__new__(cls) |
| 84 |
if self._end == 'send': |
| 85 |
cid = _channels._channel_id(cid, send=True, force=True) |
| 86 |
elif self._end == 'recv': |
| 87 |
cid = _channels._channel_id(cid, recv=True, force=True) |
| 88 |
else: |
| 89 |
raise NotImplementedError(self._end) |
| 90 |
self._id = cid |
| 91 |
return self |
| 92 |
|
| 93 |
def __repr__(self): |
| 94 |
return f'{type(self).__name__}(id={int(self._id)})' |
| 95 |
|
| 96 |
def __hash__(self): |
| 97 |
return hash(self._id) |
| 98 |
|
| 99 |
def __eq__(self, other): |
| 100 |
if isinstance(self, RecvChannel): |
| 101 |
if not isinstance(other, RecvChannel): |
| 102 |
return NotImplemented |
| 103 |
elif not isinstance(other, SendChannel): |
| 104 |
return NotImplemented |
| 105 |
return other._id == self._id |
| 106 |
|
| 107 |
# for pickling: |
| 108 |
def __reduce__(self): |
| 109 |
return (type(self), (int(self._id),)) |
| 110 |
|
| 111 |
@property |
| 112 |
def id(self): |
| 113 |
return self._id |
| 114 |
|
| 115 |
@property |
| 116 |
def _info(self): |
| 117 |
return _channels.get_info(self._id) |
| 118 |
|
| 119 |
@property |
| 120 |
def is_closed(self): |
| 121 |
return self._info.closed |
| 122 |
|
| 123 |
|
| 124 |
_NOT_SET = object() |
| 125 |
|
| 126 |
|
| 127 |
class RecvChannel(_ChannelEnd): |
| 128 |
"""The receiving end of a cross-interpreter channel.""" |
| 129 |
|
| 130 |
_end = 'recv' |
| 131 |
|
| 132 |
def recv(self, timeout=None, *, |
| 133 |
_sentinel=object(), |
| 134 |
_delay=10 / 1000, # 10 milliseconds |
| 135 |
): |
| 136 |
"""Return the next object from the channel. |
| 137 |
|
| 138 |
This blocks until an object has been sent, if none have been |
| 139 |
sent already. |
| 140 |
""" |
| 141 |
if timeout is not None: |
| 142 |
timeout = int(timeout) |
| 143 |
if timeout < 0: |
| 144 |
raise ValueError(f'timeout value must be non-negative') |
| 145 |
end = time.time() + timeout |
| 146 |
obj, unboundop = _channels.recv(self._id, _sentinel) |
| 147 |
while obj is _sentinel: |
| 148 |
time.sleep(_delay) |
| 149 |
if timeout is not None and time.time() >= end: |
| 150 |
raise TimeoutError |
| 151 |
obj, unboundop = _channels.recv(self._id, _sentinel) |
| 152 |
if unboundop is not None: |
| 153 |
assert obj is None, repr(obj) |
| 154 |
return _resolve_unbound(unboundop) |
| 155 |
return obj |
| 156 |
|
| 157 |
def recv_nowait(self, default=_NOT_SET): |
| 158 |
"""Return the next object from the channel. |
| 159 |
|
| 160 |
If none have been sent then return the default if one |
| 161 |
is provided or fail with ChannelEmptyError. Otherwise this |
| 162 |
is the same as recv(). |
| 163 |
""" |
| 164 |
if default is _NOT_SET: |
| 165 |
obj, unboundop = _channels.recv(self._id) |
| 166 |
else: |
| 167 |
obj, unboundop = _channels.recv(self._id, default) |
| 168 |
if unboundop is not None: |
| 169 |
assert obj is None, repr(obj) |
| 170 |
return _resolve_unbound(unboundop) |
| 171 |
return obj |
| 172 |
|
| 173 |
def close(self): |
| 174 |
_channels.close(self._id, recv=True) |
| 175 |
|
| 176 |
|
| 177 |
class SendChannel(_ChannelEnd): |
| 178 |
"""The sending end of a cross-interpreter channel.""" |
| 179 |
|
| 180 |
_end = 'send' |
| 181 |
|
| 182 |
# def __new__(cls, cid, *, _unbound=None): |
| 183 |
# if _unbound is None: |
| 184 |
# try: |
| 185 |
# op = _channels.get_channel_defaults(cid) |
| 186 |
# _unbound = (op,) |
| 187 |
# except ChannelNotFoundError: |
| 188 |
# _unbound = _serialize_unbound(UNBOUND) |
| 189 |
# self = super().__new__(cls, cid) |
| 190 |
# self._unbound = _unbound |
| 191 |
# return self |
| 192 |
|
| 193 |
def _set_unbound(self, op, items=None): |
| 194 |
assert not hasattr(self, '_unbound') |
| 195 |
if items is None: |
| 196 |
items = _resolve_unbound(op) |
| 197 |
unbound = (op, items) |
| 198 |
self._unbound = unbound |
| 199 |
return unbound |
| 200 |
|
| 201 |
@property |
| 202 |
def unbounditems(self): |
| 203 |
try: |
| 204 |
_, items = self._unbound |
| 205 |
except AttributeError: |
| 206 |
op, _ = _channels.get_queue_defaults(self._id) |
| 207 |
_, items = self._set_unbound(op) |
| 208 |
return items |
| 209 |
|
| 210 |
@property |
| 211 |
def is_closed(self): |
| 212 |
info = self._info |
| 213 |
return info.closed or info.closing |
| 214 |
|
| 215 |
def send(self, obj, timeout=None, *, |
| 216 |
unbounditems=None, |
| 217 |
): |
| 218 |
"""Send the object (i.e. its data) to the channel's receiving end. |
| 219 |
|
| 220 |
This blocks until the object is received. |
| 221 |
""" |
| 222 |
if unbounditems is None: |
| 223 |
unboundop = -1 |
| 224 |
else: |
| 225 |
unboundop, = _serialize_unbound(unbounditems) |
| 226 |
_channels.send(self._id, obj, unboundop, timeout=timeout, blocking=True) |
| 227 |
|
| 228 |
def send_nowait(self, obj, *, |
| 229 |
unbounditems=None, |
| 230 |
): |
| 231 |
"""Send the object to the channel's receiving end. |
| 232 |
|
| 233 |
If the object is immediately received then return True |
| 234 |
(else False). Otherwise this is the same as send(). |
| 235 |
""" |
| 236 |
if unbounditems is None: |
| 237 |
unboundop = -1 |
| 238 |
else: |
| 239 |
unboundop, = _serialize_unbound(unbounditems) |
| 240 |
# XXX Note that at the moment channel_send() only ever returns |
| 241 |
# None. This should be fixed when channel_send_wait() is added. |
| 242 |
# See bpo-32604 and gh-19829. |
| 243 |
return _channels.send(self._id, obj, unboundop, blocking=False) |
| 244 |
|
| 245 |
def send_buffer(self, obj, timeout=None, *, |
| 246 |
unbounditems=None, |
| 247 |
): |
| 248 |
"""Send the object's buffer to the channel's receiving end. |
| 249 |
|
| 250 |
This blocks until the object is received. |
| 251 |
""" |
| 252 |
if unbounditems is None: |
| 253 |
unboundop = -1 |
| 254 |
else: |
| 255 |
unboundop, = _serialize_unbound(unbounditems) |
| 256 |
_channels.send_buffer(self._id, obj, unboundop, |
| 257 |
timeout=timeout, blocking=True) |
| 258 |
|
| 259 |
def send_buffer_nowait(self, obj, *, |
| 260 |
unbounditems=None, |
| 261 |
): |
| 262 |
"""Send the object's buffer to the channel's receiving end. |
| 263 |
|
| 264 |
If the object is immediately received then return True |
| 265 |
(else False). Otherwise this is the same as send(). |
| 266 |
""" |
| 267 |
if unbounditems is None: |
| 268 |
unboundop = -1 |
| 269 |
else: |
| 270 |
unboundop, = _serialize_unbound(unbounditems) |
| 271 |
return _channels.send_buffer(self._id, obj, unboundop, blocking=False) |
| 272 |
|
| 273 |
def close(self): |
| 274 |
_channels.close(self._id, send=True) |
| 275 |
|
| 276 |
|
| 277 |
# XXX This is causing leaks (gh-110318): |
| 278 |
_channels._register_end_types(SendChannel, RecvChannel) |