@@ -229,6 +229,8 @@ def init_connection_file(self):
229
229
def init_sockets (self ):
230
230
# Create a context, a session, and the kernel sockets.
231
231
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
232
234
context = zmq .Context ()
233
235
# Uncomment this to try closing the context.
234
236
# atexit.register(context.term)
@@ -510,6 +512,12 @@ def start(self):
510
512
if not getattr (self .io_loop , '_fork_requested' , False ):
511
513
keep_running = False
512
514
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
+
513
521
pid = os .fork ()
514
522
self .io_loop ._fork_requested = False # reset for parent AND child
515
523
if pid == 0 :
@@ -569,13 +577,14 @@ def new_event_loop(self):
569
577
# NOTE: we actually start a new kernel, but once this works
570
578
# we can actually think about reusing the kernel object
571
579
self .kernel_class .clear_instance ()
580
+ self .initialize (argv = ['-f' , app_template .abs_connection_file , '--debug' ])
572
581
self .start ()
573
582
pass
574
583
else :
575
584
self .log .debug ('Parent kernel will resume' )
576
585
# keep a reference, since the will set this to None
577
586
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 ))
579
588
self .io_loop ._post_fork_callback = None
580
589
581
590
0 commit comments