Skip to content

Commit fa0b443

Browse files
committed
Remove BackgroundSocket wrapper, use IOPubThread directly
BackgroundSocket was a thin wrapper around IOPubThread that provided send/send_multipart and deprecated zmq socket attribute proxying (since ipykernel 4.3.0, 2016). Move send() to IOPubThread and remove the wrapper. - Add send() method to IOPubThread - Remove BackgroundSocket class from iostream.py - Update kernelapp.py to use iopub_thread directly - Update inprocess/ipkernel.py to use IOPubThread type - Simplify test to test IOPubThread.send directly
1 parent 770afbe commit fa0b443

File tree

4 files changed

+11
-70
lines changed

4 files changed

+11
-70
lines changed

ipykernel/inprocess/ipkernel.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from ipykernel.jsonutil import json_clean
1616
from ipykernel.zmqshell import ZMQInteractiveShell
1717

18-
from ..iostream import BackgroundSocket, IOPubThread, OutStream
18+
from ..iostream import IOPubThread, OutStream
1919
from .constants import INPROCESS_KEY
2020
from .socket import DummySocket
2121

@@ -60,11 +60,11 @@ def _default_iopub_thread(self):
6060
thread.start()
6161
return thread
6262

63-
iopub_socket: BackgroundSocket = Instance(BackgroundSocket) # type:ignore[assignment]
63+
iopub_socket: IOPubThread = Instance(IOPubThread) # type:ignore[assignment]
6464

6565
@default("iopub_socket")
6666
def _default_iopub_socket(self):
67-
return self.iopub_thread.background_socket
67+
return self.iopub_thread
6868

6969
stdin_socket = Instance(DummySocket, ())
7070

ipykernel/iostream.py

Lines changed: 4 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@ class IOPubThread:
4242
"""An object for sending IOPub messages in a background thread
4343
4444
Prevents a blocking main thread from delaying output from threads.
45-
46-
IOPubThread(pub_socket).background_socket is a Socket-API-providing object
47-
whose IO is always run in a thread.
4845
"""
4946

5047
def __init__(self, socket, pipe=False, session=False):
@@ -61,7 +58,6 @@ def __init__(self, socket, pipe=False, session=False):
6158
self.socket = socket
6259
self.session = session
6360
self._stopped = False
64-
self.background_socket = BackgroundSocket(self)
6561
self._master_pid = os.getpid()
6662
self._pipe_flag = pipe
6763
self.io_loop = IOLoop(make_current=False)
@@ -340,6 +336,10 @@ def schedule(self, f):
340336
else:
341337
f()
342338

339+
def send(self, msg, *args, **kwargs):
340+
"""Send a message to the socket."""
341+
return self.send_multipart([msg], *args, **kwargs)
342+
343343
def send_multipart(self, *args, **kwargs):
344344
"""send_multipart schedules actual zmq send in my thread.
345345
@@ -367,57 +367,6 @@ def _really_send(self, msg, *args, **kwargs):
367367
ctx.term()
368368

369369

370-
class BackgroundSocket:
371-
"""Wrapper around IOPub thread that provides zmq send[_multipart]"""
372-
373-
io_thread = None
374-
375-
def __init__(self, io_thread):
376-
"""Initialize the socket."""
377-
self.io_thread = io_thread
378-
379-
def __getattr__(self, attr):
380-
"""Wrap socket attr access for backward-compatibility"""
381-
if attr.startswith("__") and attr.endswith("__"):
382-
# don't wrap magic methods
383-
super().__getattr__(attr) # type:ignore[misc]
384-
assert self.io_thread is not None
385-
if hasattr(self.io_thread.socket, attr):
386-
warnings.warn(
387-
f"Accessing zmq Socket attribute {attr} on BackgroundSocket"
388-
f" is deprecated since ipykernel 4.3.0"
389-
f" use .io_thread.socket.{attr}",
390-
DeprecationWarning,
391-
stacklevel=2,
392-
)
393-
return getattr(self.io_thread.socket, attr)
394-
return super().__getattr__(attr) # type:ignore[misc]
395-
396-
def __setattr__(self, attr, value):
397-
"""Set an attribute on the socket."""
398-
if attr == "io_thread" or (attr.startswith("__") and attr.endswith("__")):
399-
super().__setattr__(attr, value)
400-
else:
401-
warnings.warn(
402-
f"Setting zmq Socket attribute {attr} on BackgroundSocket"
403-
f" is deprecated since ipykernel 4.3.0"
404-
f" use .io_thread.socket.{attr}",
405-
DeprecationWarning,
406-
stacklevel=2,
407-
)
408-
assert self.io_thread is not None
409-
setattr(self.io_thread.socket, attr, value)
410-
411-
def send(self, msg, *args, **kwargs):
412-
"""Send a message to the socket."""
413-
return self.send_multipart([msg], *args, **kwargs)
414-
415-
def send_multipart(self, *args, **kwargs):
416-
"""Schedule send in IO thread"""
417-
assert self.io_thread is not None
418-
return self.io_thread.send_multipart(*args, **kwargs)
419-
420-
421370
class OutStream(TextIOBase):
422371
"""A file like object that publishes the stream to a 0MQ PUB socket.
423372

ipykernel/kernelapp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,7 @@ def init_iopub(self, context):
384384
self.configure_tornado_logger()
385385
self.iopub_thread = IOPubThread(self.iopub_socket, pipe=True, session=self.session)
386386
self.iopub_thread.start()
387-
# backward-compat: wrap iopub socket API in background thread
388-
self.iopub_socket = self.iopub_thread.background_socket
387+
self.iopub_socket = self.iopub_thread
389388

390389
def init_heartbeat(self):
391390
"""start the heart beating"""

tests/test_io.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import zmq
1515
from jupyter_client.session import Session
1616

17-
from ipykernel.iostream import MASTER, BackgroundSocket, IOPubThread, OutStream
17+
from ipykernel.iostream import MASTER, IOPubThread, OutStream
1818

1919

2020
@pytest.fixture()
@@ -81,15 +81,8 @@ def test_io_thread(iopub_thread):
8181
thread._really_send(None)
8282

8383

84-
def test_background_socket(iopub_thread):
85-
sock = BackgroundSocket(iopub_thread)
86-
assert sock.__class__ == BackgroundSocket
87-
with warnings.catch_warnings():
88-
warnings.simplefilter("ignore", DeprecationWarning)
89-
sock.linger = 101
90-
assert iopub_thread.socket.linger == 101
91-
assert sock.io_thread == iopub_thread
92-
sock.send(b"hi")
84+
def test_iopub_thread_send(iopub_thread):
85+
iopub_thread.send(b"hi")
9386

9487

9588
def test_outstream(iopub_thread):

0 commit comments

Comments
 (0)