Skip to content

Commit a28afe4

Browse files
feat: write forked kernel to -forked.json and feed back info to client
1 parent 7f1045f commit a28afe4

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

ipykernel/kernelapp.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ def init_connection_file(self):
229229
def init_sockets(self):
230230
# Create a context, a session, and the kernel sockets.
231231
self.log.info("Starting the kernel at pid: %i", os.getpid())
232+
# Note: don't call .instance(), otherwise forking the kernel does not work
233+
# since the forked process will then use the same context
232234
context = zmq.Context()
233235
# Uncomment this to try closing the context.
234236
# atexit.register(context.term)
@@ -510,6 +512,12 @@ def start(self):
510512
if not getattr(self.io_loop, '_fork_requested', False):
511513
keep_running = False
512514
else:
515+
app_template = IPKernelApp(connection_file='forked.json')
516+
app_template.write_connection_file()
517+
import json
518+
with open(app_template.abs_connection_file) as f:
519+
conn = json.load(f)
520+
513521
pid = os.fork()
514522
self.io_loop._fork_requested = False # reset for parent AND child
515523
if pid == 0:
@@ -569,13 +577,14 @@ def new_event_loop(self):
569577
# NOTE: we actually start a new kernel, but once this works
570578
# we can actually think about reusing the kernel object
571579
self.kernel_class.clear_instance()
580+
self.initialize(argv=['-f', app_template.abs_connection_file, '--debug'])
572581
self.start()
573582
pass
574583
else:
575584
self.log.debug('Parent kernel will resume')
576585
# keep a reference, since the will set this to None
577586
post_fork_callback = self.io_loop._post_fork_callback
578-
self.io_loop.add_callback(lambda: post_fork_callback(pid))
587+
self.io_loop.add_callback(lambda: post_fork_callback(pid, conn))
579588
self.io_loop._post_fork_callback = None
580589

581590

ipykernel/kernelbase.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,10 @@ def fork(self, stream, ident, parent):
165165
# information up the callstack
166166
loop = ioloop.IOLoop.current()
167167
loop._fork_requested = True
168-
def post_fork_callback(pid):
168+
def post_fork_callback(pid, conn):
169169
# we might be able to pass back the port information/connection
170170
# info file here. This is just a proof of concept
171-
reply_content = json_clean({'status': 'ok', 'fork_id': pid})
171+
reply_content = json_clean({'status': 'ok', 'fork_id': pid, 'conn': conn})
172172
metadata = {}
173173
metadata = self.finish_metadata(parent, metadata, reply_content)
174174

0 commit comments

Comments
 (0)