@@ -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
0 commit comments