<commit_before>"""A basic in process kernel monitor with autorestarting.

This watches a kernel's state using KernelManager.is_alive and auto
restarts the kernel if it dies.
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2013  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import zmq
from zmq.eventloop import ioloop


from IPython.config.configurable import LoggingConfigurable
from IPython.utils.traitlets import (
    Instance, Float
)

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------

class KernelRestarter(LoggingConfigurable):
    """Monitor and autorestart a kernel."""

    loop = Instance('zmq.eventloop.ioloop.IOLoop', allow_none=False)
    def _loop_default(self):
        return ioloop.IOLoop.instance()

    kernel_manager = Instance('IPython.kernel.kernelmanager.KernelManager')

    time_to_dead = Float(3.0, config=True,
        help="""Kernel heartbeat interval in seconds."""
    )

    _pcallback = None

    def __init__(self, **kwargs):
        super(KernelRestarter, self).__init__(**kwargs)

    def start(self):
        """Start the polling of the kernel."""
        if self._pcallback is None:
            self._pcallback = ioloop.PeriodicCallback(
                self._poll, 1000*self.time_to_dead, self.ioloop
            )
        self._pcallback.start()

    def stop(self):
        """Stop the kernel polling."""
        if self._pcallback is not None:
            self._pcallback.stop()

    def clear(self):
        """Clear the underlying PeriodicCallback."""
        self.stop()
        if self._pcallback is not None:
            self._pcallback = None

    def _poll(self):
        if not self.kernel_manager.is_alive():
            self.stop()
            # This restart event should leave the connection file in place so
            # the ports are the same. Because this takes place below the
            # MappingKernelManager, the kernel_id will also remain the same.
            self.log('KernelRestarter: restarting kernel')
            self.kernel_manager.restart_kernel(now=True);
            self.start()

<commit_msg>Adding absolute import to kernelrestarter.<commit_after>"""A basic in process kernel monitor with autorestarting.

This watches a kernel's state using KernelManager.is_alive and auto
restarts the kernel if it dies.
"""

#-----------------------------------------------------------------------------
#  Copyright (C) 2013  The IPython Development Team
#
#  Distributed under the terms of the BSD License.  The full license is in
#  the file COPYING, distributed as part of this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

from __future__ import absolute_import

import zmq
from zmq.eventloop import ioloop


from IPython.config.configurable import LoggingConfigurable
from IPython.utils.traitlets import (
    Instance, Float
)

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------

class KernelRestarter(LoggingConfigurable):
    """Monitor and autorestart a kernel."""

    loop = Instance('zmq.eventloop.ioloop.IOLoop', allow_none=False)
    def _loop_default(self):
        return ioloop.IOLoop.instance()

    kernel_manager = Instance('IPython.kernel.kernelmanager.KernelManager')

    time_to_dead = Float(3.0, config=True,
        help="""Kernel heartbeat interval in seconds."""
    )

    _pcallback = None

    def __init__(self, **kwargs):
        super(KernelRestarter, self).__init__(**kwargs)

    def start(self):
        """Start the polling of the kernel."""
        if self._pcallback is None:
            self._pcallback = ioloop.PeriodicCallback(
                self._poll, 1000*self.time_to_dead, self.ioloop
            )
        self._pcallback.start()

    def stop(self):
        """Stop the kernel polling."""
        if self._pcallback is not None:
            self._pcallback.stop()

    def clear(self):
        """Clear the underlying PeriodicCallback."""
        self.stop()
        if self._pcallback is not None:
            self._pcallback = None

    def _poll(self):
        if not self.kernel_manager.is_alive():
            self.stop()
            # This restart event should leave the connection file in place so
            # the ports are the same. Because this takes place below the
            # MappingKernelManager, the kernel_id will also remain the same.
            self.log('KernelRestarter: restarting kernel')
            self.kernel_manager.restart_kernel(now=True);
            self.start()


{'fraction_non_alphanumeric': 0.2491946181542543, 'fraction_numerical': 0.0037900322152738296, 'mean_word_length': 1.5241511238641798, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6064234', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1225, 'n_words': 378}