<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from contextlib import contextmanager <EOL> import os <EOL> import re <EOL> import signal <EOL> import sys <EOL> import time <EOL> import warnings <EOL> try : <EOL> from queue import Empty <EOL> except ImportError : <EOL> from Queue import Empty <EOL> import zmq <EOL> from ipython_genutils . importstring import import_item <EOL> from . localinterfaces import is_local_ip , local_ips <EOL> from traitlets import ( <EOL> Any , Instance , Unicode , List , Bool , Type , DottedObjectName <EOL> ) <EOL> from jupyter_client import ( <EOL> launch_kernel , <EOL> kernelspec , <EOL> ) <EOL> from . connect import ConnectionFileMixin <EOL> from . session import Session <EOL> from . managerabc import ( <EOL> KernelManagerABC <EOL> ) <EOL> class KernelManager ( ConnectionFileMixin ) : <EOL> """<STR_LIT>""" <EOL> context = Instance ( zmq . Context ) <EOL> def _context_default ( self ) : <EOL> return zmq . Context . instance ( ) <EOL> client_class = DottedObjectName ( '<STR_LIT>' ) <EOL> client_factory = Type ( klass = '<STR_LIT>' ) <EOL> def _client_factory_default ( self ) : <EOL> return import_item ( self . client_class ) <EOL> def _client_class_changed ( self , name , old , new ) : <EOL> self . client_factory = import_item ( str ( new ) ) <EOL> kernel = Any ( ) <EOL> kernel_spec_manager = Instance ( kernelspec . KernelSpecManager ) <EOL> def _kernel_spec_manager_default ( self ) : <EOL> return kernelspec . KernelSpecManager ( data_dir = self . data_dir ) <EOL> def _kernel_spec_manager_changed ( self ) : <EOL> self . _kernel_spec = None <EOL> kernel_name = Unicode ( kernelspec . NATIVE_KERNEL_NAME ) <EOL> def _kernel_name_changed ( self , name , old , new ) : <EOL> self . _kernel_spec = None <EOL> if new == '<STR_LIT>' : <EOL> self . kernel_name = kernelspec . NATIVE_KERNEL_NAME <EOL> _kernel_spec = None <EOL> @ property <EOL> def kernel_spec ( self ) : <EOL> if self . _kernel_spec is None : <EOL> self . _kernel_spec = self . kernel_spec_manager . get_kernel_spec ( self . kernel_name ) <EOL> return self . _kernel_spec <EOL> kernel_cmd = List ( Unicode ( ) , config = True , <EOL> help = """<STR_LIT>""" <EOL> ) <EOL> def _kernel_cmd_changed ( self , name , old , new ) : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> @ property <EOL> def ipykernel ( self ) : <EOL> return self . kernel_name in { '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' } <EOL> _launch_args = Any ( ) <EOL> _control_socket = Any ( ) <EOL> _restarter = Any ( ) <EOL> autorestart = Bool ( True , config = True , <EOL> help = """<STR_LIT>""" <EOL> ) <EOL> def __del__ ( self ) : <EOL> self . _close_control_socket ( ) <EOL> self . cleanup_connection_file ( ) <EOL> def start_restarter ( self ) : <EOL> pass <EOL> def stop_restarter ( self ) : <EOL> pass <EOL> def add_restart_callback ( self , callback , event = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if self . _restarter is None : <EOL> return <EOL> self . _restarter . add_callback ( callback , event ) <EOL> def remove_restart_callback ( self , callback , event = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if self . _restarter is None : <EOL> return <EOL> self . _restarter . remove_callback ( callback , event ) <EOL> def client ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> kw = { } <EOL> kw . update ( self . get_connection_info ( session = True ) ) <EOL> kw . update ( dict ( <EOL> connection_file = self . connection_file , <EOL> parent = self , <EOL> ) ) <EOL> kw . update ( kwargs ) <EOL> return self . client_factory ( ** kw ) <EOL> def format_kernel_cmd ( self , extra_arguments = None ) : <EOL> """<STR_LIT>""" <EOL> extra_arguments = extra_arguments or [ ] <EOL> if self . kernel_cmd : <EOL> cmd = self . kernel_cmd + extra_arguments <EOL> else : <EOL> cmd = self . kernel_spec . argv + extra_arguments <EOL> ns = dict ( connection_file = self . connection_file , <EOL> prefix = sys . prefix , <EOL> ) <EOL> ns . update ( self . _launch_args ) <EOL> pat = re . compile ( r'<STR_LIT>' ) <EOL> def from_ns ( match ) : <EOL> """<STR_LIT>""" <EOL> return ns . get ( match . group ( <NUM_LIT:1> ) , match . group ( ) ) <EOL> return [ pat . sub ( from_ns , arg ) for arg in cmd ] <EOL> def _launch_kernel ( self , kernel_cmd , ** kw ) : <EOL> """<STR_LIT>""" <EOL> return launch_kernel ( kernel_cmd , ** kw ) <EOL> def _connect_control_socket ( self ) : <EOL> if self . _control_socket is None : <EOL> self . _control_socket = self . connect_control ( ) <EOL> self . _control_socket . linger = <NUM_LIT:100> <EOL> def _close_control_socket ( self ) : <EOL> if self . _control_socket is None : <EOL> return <EOL> self . _control_socket . close ( ) <EOL> self . _control_socket = None <EOL> def start_kernel ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if self . transport == '<STR_LIT>' and not is_local_ip ( self . ip ) : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % local_ips ( ) <EOL> ) <EOL> self . write_connection_file ( ) <EOL> self . _launch_args = kw . copy ( ) <EOL> extra_arguments = kw . pop ( '<STR_LIT>' , [ ] ) <EOL> kernel_cmd = self . format_kernel_cmd ( extra_arguments = extra_arguments ) <EOL> env = kw . pop ( '<STR_LIT>' , os . environ ) . copy ( ) <EOL> env . pop ( '<STR_LIT>' , None ) <EOL> if not self . kernel_cmd : <EOL> env . update ( self . kernel_spec . env or { } ) <EOL> self . log . debug ( "<STR_LIT>" , kernel_cmd ) <EOL> self . kernel = self . _launch_kernel ( kernel_cmd , env = env , <EOL> ** kw ) <EOL> self . start_restarter ( ) <EOL> self . _connect_control_socket ( ) <EOL> def request_shutdown ( self , restart = False ) : <EOL> """<STR_LIT>""" <EOL> content = dict ( restart = restart ) <EOL> msg = self . session . msg ( "<STR_LIT>" , content = content ) <EOL> self . session . send ( self . _control_socket , msg ) <EOL> def finish_shutdown ( self , waittime = <NUM_LIT:1> , pollinterval = <NUM_LIT:0.1> ) : <EOL> """<STR_LIT>""" <EOL> for i in range ( int ( waittime / pollinterval ) ) : <EOL> if self . is_alive ( ) : <EOL> time . sleep ( pollinterval ) <EOL> else : <EOL> break <EOL> else : <EOL> if self . has_kernel : <EOL> self . _kill_kernel ( ) <EOL> def cleanup ( self , connection_file = True ) : <EOL> """<STR_LIT>""" <EOL> if connection_file : <EOL> self . cleanup_connection_file ( ) <EOL> self . cleanup_ipc_files ( ) <EOL> self . _close_control_socket ( ) <EOL> def shutdown_kernel ( self , now = False , restart = False ) : <EOL> """<STR_LIT>""" <EOL> self . stop_restarter ( ) <EOL> if now : <EOL> self . _kill_kernel ( ) <EOL> else : <EOL> self . request_shutdown ( restart = restart ) <EOL> self . finish_shutdown ( ) <EOL> self . cleanup ( connection_file = not restart ) <EOL> def restart_kernel ( self , now = False , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if self . _launch_args is None : <EOL> raise RuntimeError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> else : <EOL> self . shutdown_kernel ( now = now , restart = True ) <EOL> self . _launch_args . update ( kw ) <EOL> self . start_kernel ( ** self . _launch_args ) <EOL> @ property <EOL> def has_kernel ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . kernel is not None <EOL> def _kill_kernel ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . has_kernel : <EOL> try : <EOL> self . kernel . kill ( ) <EOL> except OSError as e : <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> if e . winerror != <NUM_LIT:5> : <EOL> raise <EOL> else : <EOL> from errno import ESRCH <EOL> if e . errno != ESRCH : <EOL> raise <EOL> self . kernel . wait ( ) <EOL> self . kernel = None <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def interrupt_kernel ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . has_kernel : <EOL> if sys . platform == '<STR_LIT:win32>' : <EOL> from . win_interrupt import send_interrupt <EOL> send_interrupt ( self . kernel . win32_interrupt_event ) <EOL> else : <EOL> self . signal_kernel ( signal . SIGINT ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def signal_kernel ( self , signum ) : <EOL> """<STR_LIT>""" <EOL> if self . has_kernel : <EOL> if hasattr ( os , "<STR_LIT>" ) and hasattr ( os , "<STR_LIT>" ) : <EOL> try : <EOL> pgid = os . getpgid ( self . kernel . pid ) <EOL> os . killpg ( pgid , signum ) <EOL> return <EOL> except OSError : <EOL> pass <EOL> self . kernel . send_signal ( signum ) <EOL> else : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> def is_alive ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . has_kernel : <EOL> if self . kernel . poll ( ) is None : <EOL> return True <EOL> else : <EOL> return False <EOL> else : <EOL> return False <EOL> KernelManagerABC . register ( KernelManager ) <EOL> def start_new_kernel ( startup_timeout = <NUM_LIT> , kernel_name = '<STR_LIT>' , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> km = KernelManager ( kernel_name = kernel_name ) <EOL> km . start_kernel ( ** kwargs ) <EOL> kc = km . client ( ) <EOL> kc . start_channels ( ) <EOL> try : <EOL> kc . wait_for_ready ( timeout = startup_timeout ) <EOL> except RuntimeError : <EOL> kc . stop_channels ( ) <EOL> km . shutdown_kernel ( ) <EOL> raise <EOL> return km , kc <EOL> @ contextmanager <EOL> def run_kernel ( ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> km , kc = start_new_kernel ( ** kwargs ) <EOL> try : <EOL> yield kc <EOL> finally : <EOL> kc . stop_channels ( ) <EOL> km . shutdown_kernel ( now = True ) </s>
