Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid kernel failures with multiple processes #437

Merged
merged 2 commits into from
May 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ python:
- "3.7-dev"
- 3.6
- 3.5
- 3.4
- 2.7
sudo: false
install:
Expand Down
2 changes: 1 addition & 1 deletion jupyter_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class KernelClient(ConnectionFileMixin):
# The PyZMQ Context to use for communication with the kernel.
context = Instance(zmq.Context)
def _context_default(self):
return zmq.Context.instance()
return zmq.Context()

# The classes to use for the various channels
shell_channel_class = Type(ChannelABC)
Expand Down
120 changes: 119 additions & 1 deletion jupyter_client/tests/test_kernelmanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from subprocess import PIPE
import sys
import time
import threading
import multiprocessing as mp
import pytest
from unittest import TestCase

from traitlets.config.loader import Config
Expand All @@ -28,7 +31,7 @@ def setUp(self):

def tearDown(self):
self.env_patch.stop()

def _install_test_kernel(self):
kernel_dir = pjoin(paths.jupyter_data_dir(), 'kernels', 'signaltest')
os.makedirs(kernel_dir)
Expand Down Expand Up @@ -127,3 +130,118 @@ def test_start_new_kernel(self):

self.assertTrue(km.is_alive())
self.assertTrue(kc.is_alive())

@pytest.mark.parallel
class TestParallel:

@pytest.fixture(autouse=True)
def env(self):
env_patch = test_env()
env_patch.start()
yield
env_patch.stop()

@pytest.fixture(params=['tcp', 'ipc'])
def transport(self, request):
return request.param

@pytest.fixture
def config(self, transport):
c = Config()
c.transport = transport
if transport == 'ipc':
c.ip = 'test'
return c

def _install_test_kernel(self):
kernel_dir = pjoin(paths.jupyter_data_dir(), 'kernels', 'signaltest')
os.makedirs(kernel_dir)
with open(pjoin(kernel_dir, 'kernel.json'), 'w') as f:
f.write(json.dumps({
'argv': [sys.executable,
'-m', 'jupyter_client.tests.signalkernel',
'-f', '{connection_file}'],
'display_name': "Signal Test Kernel",
}))

def test_start_sequence_kernels(self, config):
"""Ensure that a sequence of kernel startups doesn't break anything."""

self._install_test_kernel()
self._run_signaltest_lifecycle(config)
self._run_signaltest_lifecycle(config)
self._run_signaltest_lifecycle(config)

def test_start_parallel_thread_kernels(self, config):
self._install_test_kernel()
self._run_signaltest_lifecycle(config)

thread = threading.Thread(target=self._run_signaltest_lifecycle, args=(config,))
thread2 = threading.Thread(target=self._run_signaltest_lifecycle, args=(config,))
try:
thread.start()
thread2.start()
finally:
thread.join()
thread2.join()

def test_start_parallel_process_kernels(self, config):
self._install_test_kernel()

self._run_signaltest_lifecycle(config)
thread = threading.Thread(target=self._run_signaltest_lifecycle, args=(config,))
proc = mp.Process(target=self._run_signaltest_lifecycle, args=(config,))
try:
thread.start()
proc.start()
finally:
thread.join()
proc.join()

assert proc.exitcode == 0

def test_start_sequence_process_kernels(self, config):
self._install_test_kernel()
self._run_signaltest_lifecycle(config)
proc = mp.Process(target=self._run_signaltest_lifecycle, args=(config,))
try:
proc.start()
finally:
proc.join()

assert proc.exitcode == 0

def _prepare_kernel(self, km, startup_timeout=TIMEOUT, **kwargs):
km.start_kernel(**kwargs)
kc = km.client()
kc.start_channels()
try:
kc.wait_for_ready(timeout=startup_timeout)
except RuntimeError:
kc.stop_channels()
km.shutdown_kernel()
raise

return kc

def _run_signaltest_lifecycle(self, config=None):
km = KernelManager(config=config, kernel_name='signaltest')
kc = self._prepare_kernel(km, stdout=PIPE, stderr=PIPE)

def execute(cmd):
kc.execute(cmd)
reply = kc.get_shell_msg(TIMEOUT)
content = reply['content']
assert content['status'] == 'ok'
return content

execute("start")
assert km.is_alive()
execute('check')
assert km.is_alive()

km.restart_kernel(now=True)
assert km.is_alive()
execute('check')

km.shutdown_kernel()
9 changes: 4 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
import sys

v = sys.version_info
if v[:2] < (2,7) or (v[0] >= 3 and v[:2] < (3,4)):
error = "ERROR: %s requires Python version 2.7 or 3.4 or above." % name
if v[:2] < (2, 7) or (v[0] >= 3 and v[:2] < (3, 5)):
error = "ERROR: %s requires Python version 2.7 or 3.5 or above." % name
print(error, file=sys.stderr)
sys.exit(1)

Expand Down Expand Up @@ -94,10 +94,9 @@ def run(self):
'entrypoints',
'tornado>=4.1',
],
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*',
python_requires='>=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*',
extras_require = {
'test': ['ipykernel', 'ipython', 'mock'],
'test:(python_version >= "3.4" or python_version == "2.7")': ['pytest'],
'test': ['ipykernel', 'ipython', 'mock', 'pytest'],
},
cmdclass = {
'bdist_egg': bdist_egg if 'bdist_egg' in sys.argv else bdist_egg_disabled,
Expand Down