Skip to content

Commit

Permalink
Merge pull request #25 from ibm-watson-data-lab/issue23
Browse files Browse the repository at this point in the history
terminate Node.js process on Kernel shutdown - fixes issue #23
  • Loading branch information
glynnbird authored Feb 9, 2018
2 parents 02fbe1a + 732655d commit b670911
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
25 changes: 20 additions & 5 deletions pixiedust_node/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from IPython.core.magic import (Magics, magics_class, cell_magic)
from IPython.display import display, HTML
from IPython.core.error import TryNext
import warnings
from .node import Node, Npm
import os
Expand All @@ -25,7 +26,7 @@
@magics_class
class PixiedustNodeMagics(Magics):

def __init__(self, shell):
def __init__(self, shell, node):
super(PixiedustNodeMagics,self).__init__(shell=shell)
display(HTML(
"""
Expand All @@ -37,9 +38,7 @@ def __init__(self, shell):
</div>
"""
))
# create Node.js sub-process
path = os.path.join(__path__[0], 'pixiedustNodeRepl.js')
self.n = Node(path)
self.n = node
ShellAccess.npm = Npm()
ShellAccess.node = self.n

Expand All @@ -48,11 +47,27 @@ def node(self, line, cell):
# write the cell contents to the Node.js process
self.n.write(cell)

# call once when the Kernel shuts down
def shutdown_hook(ipython):
node.terminate()
raise TryNext

try:
with warnings.catch_warnings():
warnings.simplefilter("ignore")
get_ipython().register_magics(PixiedustNodeMagics)
ip = get_ipython()

# start up a Node.js sub-process running a REPL
path = os.path.join(__path__[0], 'pixiedustNodeRepl.js')
node = Node(path)

# pass the node process to the Node magics
magics = PixiedustNodeMagics(ip, node)
ip.register_magics(magics)

# register for shutdown hook
ip.set_hook('shutdown_hook', shutdown_hook)

except NameError:
# IPython not available we must be in a spark executor\
pass
5 changes: 4 additions & 1 deletion pixiedust_node/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,14 +197,17 @@ def __init__(self, path):
# process that runs the Node.js code
args = (self.node_path, path)
self.ps = self.popen(args)
# print ("Node process id", self.ps.pid)
print ("Node process id", self.ps.pid)

# create thread to read this process's output
NodeStdReader(self.ps)

# watch Python variables for changes
self.vw = VarWatcher(get_ipython(), self.ps)

def terminate(self):
self.ps.terminate()

def write(self, s):
self.ps.stdin.write(s)
self.ps.stdin.write("\r\n")
Expand Down

0 comments on commit b670911

Please sign in to comment.