euporie.core.kernel.jupyter_manager
Contain classes relating to kernel management.
Functions
Set the default kernel provisioner to euporie's logging provisioner. |
Classes
|
An async kernel manager. |
|
Kernel Manager subclass. |
|
|
|
A Jupyter kernel provisionser which logs kernel output. |
- class euporie.core.kernel.jupyter_manager.EuporieKernelManager(**kwargs: Any)
Bases:
AsyncKernelManager
Kernel Manager subclass.
jupyter_client
replaces a plainpython
command with the current executable, but this is not desirable if the client is running in its own prefix (e.g. withpipx
). We work around this here.See https://github.com/jupyter/jupyter_client/issues/949
- add_restart_callback(callback: Callable, event: str = 'restart') None
Register a callback to be called when a kernel is restarted
- autorestart: Bool
Should we autorestart the kernel if it dies.
- blocking_class
A trait whose value must be a subclass of a specified class.
- blocking_client() BlockingKernelClient
Make a blocking client connected to my kernel
- cache_ports: Bool
True if the MultiKernelManager should cache ports for this KernelManager instance
- classmethod class_config_rst_doc() str
Generate rST documentation for this class’ config options.
Excludes traits defined on parent classes.
- classmethod class_config_section(classes: Sequence[type[HasTraits]] | None = None) str
Get the config section for this class.
- Parameters:
classes (list, optional) – The list of other classes in the config file. Used to reduce redundant information.
- classmethod class_get_help(inst: HasTraits | None = None) str
Get the help string for this class in ReST format.
If inst is given, its current trait values will be used in place of class defaults.
- classmethod class_get_trait_help(trait: TraitType[Any, Any], inst: HasTraits | None = None, helptext: str | None = None) str
Get the helptext string for a single trait.
- Parameters:
inst – If given, its current trait values will be used in place of the class default.
helptext – If not given, uses the help attribute of the current trait.
- classmethod class_own_trait_events(name: str) dict[str, EventHandler]
Get a dict of all event handlers defined on this class, not a parent.
Works like
event_handlers
, except for excluding traits from parents.
- classmethod class_own_traits(**metadata: Any) dict[str, TraitType[Any, Any]]
Get a dict of all the traitlets defined on this class, not a parent.
Works like class_traits, except for excluding traits from parents.
- classmethod class_print_help(inst: HasTraits | None = None) None
Get the help string for a single trait and print it.
- classmethod class_trait_names(**metadata: Any) list[str]
Get a list of all the names of this class’ traits.
This method is just like the
trait_names()
method, but is unbound.
- classmethod class_traits(**metadata: Any) dict[str, TraitType[Any, Any]]
Get a
dict
of all the traits of this class. The dictionary is keyed on the name and the values are the TraitType objects.This method is just like the
traits()
method, but is unbound.The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.
The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. If a metadata key doesn’t exist, None will be passed to the function.
- cleanup_connection_file() None
Cleanup connection file if we wrote it
Will not raise if the connection file was already removed somehow.
- cleanup_random_ports() None
Forgets randomly assigned port numbers and cleans up the connection file.
Does nothing if no port numbers have been randomly assigned. In particular, does nothing unless the transport is tcp.
- async cleanup_resources(restart: bool = False) None
Clean up resources when the kernel is shut down
- client_class: DottedObjectName
A string holding a valid dotted object name in Python, such as A.b3._c
- client_factory: Type
A trait whose value must be a subclass of a specified class.
- config
A trait whose value must be an instance of a specified class.
The value can also be an instance of a subclass of the specified class.
Subclasses can declare default classes by overriding the klass attribute
- connect_control(identity: bytes | None = None) Socket
return zmq Socket connected to the Control channel
- connect_hb(identity: bytes | None = None) Socket
return zmq Socket connected to the Heartbeat channel
- connect_iopub(identity: bytes | None = None) Socket
return zmq Socket connected to the IOPub channel
- connect_shell(identity: bytes | None = None) Socket
return zmq Socket connected to the Shell channel
- connect_stdin(identity: bytes | None = None) Socket
return zmq Socket connected to the StdIn channel
- connection_file
kernel-<pid>.json]
This file will contain the IP, ports, and authentication key needed to connect clients to this kernel. By default, this file will be created in the security dir of the current profile, but can be specified by absolute path.
- Type:
JSON file in which to store connection info [default
- context: Instance
A trait whose value must be an instance of a specified class.
The value can also be an instance of a subclass of the specified class.
Subclasses can declare default classes by overriding the klass attribute
- control_port
random]
- Type:
set the control (ROUTER) port [default
- property cross_validation_lock: Any
A contextmanager for running a block with our cross validation lock set to True.
At the end of the block, the lock’s value is restored to its value prior to entering the block.
- async finish_shutdown(waittime: float | None = None, pollinterval: float = 0.1, restart: bool = False) None
Wait for kernel shutdown, then kill process if it doesn’t shutdown.
This does not send shutdown requests - use
request_shutdown()
first.
- format_kernel_cmd(extra_arguments: list[str] | None = None) list[str]
Replace templated args (e.g. {connection_file}).
- get_connection_info(session: bool = False) Dict[str, int | str | bytes]
Return the connection info as a dict
- Parameters:
session (bool [default: False]) – If True, return our session object will be included in the connection info. If False (default), the configuration parameters of our session object will be included, rather than the session object itself.
- Returns:
connect_info – dictionary of connection information.
- Return type:
- hb_port
random]
- Type:
set the heartbeat port [default
- hold_trait_notifications() Any
Context manager for bundling trait change notifications and cross validation.
Use this when doing multiple trait assignments (init, config), to avoid race conditions in trait notifiers requesting other trait values. All trait notifications will fire after all values have been assigned.
- async interrupt_kernel() None
Interrupts the kernel by sending it a signal.
Unlike
signal_kernel
, this operation is well supported on all platforms.
- iopub_port
random]
- Type:
set the iopub (PUB) port [default
- ip
Set the kernel’s IP address [default localhost]. If the IP address is something other than localhost, then Consoles on other machines will be able to connect to the Kernel, so be careful!
- kernel_spec_manager: Instance
A trait whose value must be an instance of a specified class.
The value can also be an instance of a subclass of the specified class.
Subclasses can declare default classes by overriding the klass attribute
- load_connection_file(connection_file: str | None = None) None
Load connection info from JSON dict in self.connection_file.
- Parameters:
connection_file (unicode, optional) – Path to connection file to load. If unspecified, use self.connection_file
- load_connection_info(info: Dict[str, int | str | bytes]) None
Load connection info from a dict containing connection info.
Typically this data comes from a connection file and is called by load_connection_file.
- Parameters:
info (dict) – Dictionary containing connection_info. See the connection_file spec for details.
- log
Logger or LoggerAdapter instance
- observe(handler: Callable[[...], Any], names: Sentinel | str | Iterable[Sentinel | str] = traitlets.All, type: Sentinel | str = 'change') None
Setup a handler to be called when a trait changes.
This is used to setup dynamic notifications of trait changes.
- Parameters:
handler (callable) – A callable that is called when a trait changes. Its signature should be
handler(change)
, wherechange
is a dictionary. The change dictionary at least holds a ‘type’ key. *type
: the type of notification. Other keys may be passed depending on the value of ‘type’. In the case where type is ‘change’, we also have the following keys: *owner
: the HasTraits instance *old
: the old value of the modified trait attribute *new
: the new value of the modified trait attribute *name
: the name of the modified trait attribute.names (list, str, All) – If names is All, the handler will apply to all traits. If a list of str, handler will apply to all names in the list. If a str, the handler will apply just to that name.
type (str, All (default: 'change')) – The type of notification to filter by. If equal to All, then all notifications are passed to the observe handler.
- on_trait_change(handler: EventHandler | None = None, name: Sentinel | str | None = None, remove: bool = False) None
DEPRECATED: Setup a handler to be called when a trait changes.
This is used to setup dynamic notifications of trait changes.
Static handlers can be created by creating methods on a HasTraits subclass with the naming convention ‘_[traitname]_changed’. Thus, to create static handler for the trait ‘a’, create the method _a_changed(self, name, old, new) (fewer arguments can be used, see below).
If remove is True and handler is not specified, all change handlers for the specified name are uninstalled.
- Parameters:
handler (callable, None) – A callable that is called when a trait changes. Its signature can be handler(), handler(name), handler(name, new), handler(name, old, new), or handler(name, old, new, self).
name (list, str, None) – If None, the handler will apply to all traits. If a list of str, handler will apply to all names in the list. If a str, the handler will apply just to that name.
remove (bool) – If False (the default), then install the handler. If True then unintall it.
- parent
A trait whose value must be an instance of a specified class.
The value can also be an instance of a subclass of the specified class.
Subclasses can declare default classes by overriding the klass attribute
- async post_start_kernel(**kw: Any) None
Performs any post startup tasks relative to the kernel.
- Parameters:
**kw (optional) – keyword arguments that were used in the kernel process’s launch.
- async pre_start_kernel(**kw: Any) Tuple[List[str], Dict[str, Any]]
Prepares a kernel for startup in a separate process.
If random ports (port=0) are being used, this method must be called before the channels are created.
- Parameters:
**kw (optional) – keyword arguments that are passed down to build the kernel_cmd and launching the kernel (e.g. Popen kwargs).
- provisioner: t.Optional[KernelProvisionerBase] = None
- property ready: Future | Future
A future that resolves when the kernel process has started for the first time
- remove_restart_callback(callback: Callable, event: str = 'restart') None
Unregister a callback to be called when a kernel is restarted
- async restart_kernel(now: bool = False, newports: bool = False, **kw: Any) None
Restarts a kernel with the arguments that were used to launch it.
- Parameters:
now (bool, optional) –
If True, the kernel is forcefully restarted immediately, without having a chance to do any cleanup action. Otherwise the kernel is given 1s to clean up before a forceful restart is issued.
In all cases the kernel is restarted, the only difference is whether it is given a chance to perform a clean shutdown or not.
newports (bool, optional) – If the old kernel was launched with random ports, this flag decides whether the same ports and connection file will be used again. If False, the same ports and connection file are used. This is the default. If True, new random port numbers are chosen and a new connection file is written. It is still possible that the newly chosen random port numbers happen to be the same as the old ones.
**kw (optional) – Any options specified here will overwrite those used to launch the kernel.
- session
A trait whose value must be an instance of a specified class.
The value can also be an instance of a subclass of the specified class.
Subclasses can declare default classes by overriding the klass attribute
- set_trait(name: str, value: Any) None
Forcibly sets trait attribute, including read-only attributes.
- shell_port
random]
- Type:
set the shell (ROUTER) port [default
- async shutdown_kernel(now: bool = False, restart: bool = False) None
Attempts to stop the kernel process cleanly.
This attempts to shutdown the kernels cleanly by:
Sending it a shutdown message over the control channel.
If that fails, the kernel is shutdown forcibly by sending it a signal.
- shutdown_wait_time: Float
Time to wait for a kernel to terminate before killing it, in seconds. When a shutdown request is initiated, the kernel will be immediately sent an interrupt (SIGINT), followedby a shutdown_request message, after 1/2 of shutdown_wait_time`it will be sent a terminate (SIGTERM) request, and finally at the end of `shutdown_wait_time will be killed (SIGKILL). terminate and kill may be equivalent on windows. Note that this value can beoverridden by the in-use kernel provisioner since shutdown times mayvary by provisioned environment.
- async signal_kernel(signum: int) None
Sends a signal to the process group of the kernel (this usually includes the kernel and any subprocesses spawned by the kernel).
Note that since only SIGTERM is supported on Windows, this function is only useful on Unix systems.
- async start_kernel(**kw: Any) None
Starts a kernel on this host in a separate process.
If random ports (port=0) are being used, this method must be called before the channels are created.
- Parameters:
**kw (optional) – keyword arguments that are passed down to build the kernel_cmd and launching the kernel (e.g. Popen kwargs).
- stdin_port
random]
- Type:
set the stdin (ROUTER) port [default
- trait_defaults(*names: str, **metadata: Any) dict[str, Any] | Sentinel
Return a trait’s default value or a dictionary of them
Notes
Dynamically generated default values may depend on the current state of the object.
- classmethod trait_events(name: str | None = None) dict[str, EventHandler]
Get a
dict
of all the event handlers of this class.- Parameters:
name (str (default: None)) – The name of a trait of this class. If name is
None
then all the event handlers of this class will be returned instead.- Return type:
The event handlers associated with a trait name, or all event handlers.
- trait_has_value(name: str) bool
Returns True if the specified trait has a value.
This will return false even if
getattr
would return a dynamically generated default value. These default values will be recognized as existing only after they have been generated.Example
class MyClass(HasTraits): i = Int() mc = MyClass() assert not mc.trait_has_value("i") mc.i # generates a default value assert mc.trait_has_value("i")
- trait_metadata(traitname: str, key: str, default: Any = None) Any
Get metadata values for trait by key.
- trait_values(**metadata: Any) dict[str, Any]
A
dict
of trait names and their values.The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. If a metadata key doesn’t exist, None will be passed to the function.
- Return type:
A
dict
of trait names and their values.
Notes
Trait values are retrieved via
getattr
, any exceptions raised by traits or the operations they may trigger will result in the absence of a trait value in the resultdict
.
- traits(**metadata: Any) dict[str, TraitType[Any, Any]]
Get a
dict
of all the traits of this class. The dictionary is keyed on the name and the values are the TraitType objects.The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.
The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. If a metadata key doesn’t exist, None will be passed to the function.
- transport
An enum of strings where the case should be ignored.
- unobserve(handler: Callable[[...], Any], names: Sentinel | str | Iterable[Sentinel | str] = traitlets.All, type: Sentinel | str = 'change') None
Remove a trait change handler.
This is used to unregister handlers to trait change notifications.
- Parameters:
handler (callable) – The callable called when a trait attribute changes.
names (list, str, All (default: All)) – The names of the traits for which the specified handler should be uninstalled. If names is All, the specified handler is uninstalled from the list of notifiers corresponding to all changes.
type (str or All (default: 'change')) – The type of notification to filter by. If All, the specified handler is uninstalled from the list of notifiers corresponding to all types.
- unobserve_all(name: str | Any = traitlets.All) None
Remove trait change handlers of any type for the specified name. If name is not specified, removes all trait notifiers.
- update_env(*, env: Dict[str, str]) None
Allow to update the environment of a kernel manager.
This will take effect only after kernel restart when the new env is passed to the new kernel.
This is useful as some of the information of the current kernel reflect the state of the session that started it, and those session information (like the attach file path, or name), are mutable.
- class euporie.core.kernel.jupyter_manager.LoggingLocalProvisioner(**kwargs: Any)
Bases:
LocalProvisioner
A Jupyter kernel provisionser which logs kernel output.
- classmethod class_config_rst_doc() str
Generate rST documentation for this class’ config options.
Excludes traits defined on parent classes.
- classmethod class_config_section(classes: Sequence[type[HasTraits]] | None = None) str
Get the config section for this class.
- Parameters:
classes (list, optional) – The list of other classes in the config file. Used to reduce redundant information.
- classmethod class_get_help(inst: HasTraits | None = None) str
Get the help string for this class in ReST format.
If inst is given, its current trait values will be used in place of class defaults.
- classmethod class_get_trait_help(trait: TraitType[Any, Any], inst: HasTraits | None = None, helptext: str | None = None) str
Get the helptext string for a single trait.
- Parameters:
inst – If given, its current trait values will be used in place of the class default.
helptext – If not given, uses the help attribute of the current trait.
- classmethod class_own_trait_events(name: str) dict[str, EventHandler]
Get a dict of all event handlers defined on this class, not a parent.
Works like
event_handlers
, except for excluding traits from parents.
- classmethod class_own_traits(**metadata: Any) dict[str, TraitType[Any, Any]]
Get a dict of all the traitlets defined on this class, not a parent.
Works like class_traits, except for excluding traits from parents.
- classmethod class_print_help(inst: HasTraits | None = None) None
Get the help string for a single trait and print it.
- classmethod class_trait_names(**metadata: Any) list[str]
Get a list of all the names of this class’ traits.
This method is just like the
trait_names()
method, but is unbound.
- classmethod class_traits(**metadata: Any) dict[str, TraitType[Any, Any]]
Get a
dict
of all the traits of this class. The dictionary is keyed on the name and the values are the TraitType objects.This method is just like the
traits()
method, but is unbound.The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.
The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. If a metadata key doesn’t exist, None will be passed to the function.
- async cleanup(restart: bool = False) None
Clean up the resources used by the provisioner and optionally restart.
- config
A trait whose value must be an instance of a specified class.
The value can also be an instance of a subclass of the specified class.
Subclasses can declare default classes by overriding the klass attribute
- connection_info: KernelConnectionInfo = {}
- property cross_validation_lock: Any
A contextmanager for running a block with our cross validation lock set to True.
At the end of the block, the lock’s value is restored to its value prior to entering the block.
- async get_provisioner_info() Dict
Captures the base information necessary for persistence relative to this instance.
- get_shutdown_wait_time(recommended: float = 5.0) float
Returns the time allowed for a complete shutdown. This may vary by provisioner.
This method is called from KernelManager.finish_shutdown() during the graceful phase of its kernel shutdown sequence.
The recommended value will typically be what is configured in the kernel manager.
- get_stable_start_time(recommended: float = 10.0) float
Returns the expected upper bound for a kernel (re-)start to complete. This may vary by provisioner.
The recommended value will typically be what is configured in the kernel restarter.
- property has_process: bool
Returns true if this provisioner is currently managing a process.
This property is asserted to be True immediately following a call to the provisioner’s
launch_kernel()
method.
- hold_trait_notifications() Any
Context manager for bundling trait change notifications and cross validation.
Use this when doing multiple trait assignments (init, config), to avoid race conditions in trait notifiers requesting other trait values. All trait notifications will fire after all values have been assigned.
- ip = None
- kernel_spec: Any
A trait whose value must be an instance of a specified class.
The value can also be an instance of a subclass of the specified class.
Subclasses can declare default classes by overriding the klass attribute
- async launch_kernel(cmd: list[str], **kwargs: Any) KernelConnectionInfo
Launch a kernel with a command.
- async load_provisioner_info(provisioner_info: Dict) None
Loads the base information necessary for persistence relative to this instance.
- log
Logger or LoggerAdapter instance
- observe(handler: Callable[[...], Any], names: Sentinel | str | Iterable[Sentinel | str] = traitlets.All, type: Sentinel | str = 'change') None
Setup a handler to be called when a trait changes.
This is used to setup dynamic notifications of trait changes.
- Parameters:
handler (callable) – A callable that is called when a trait changes. Its signature should be
handler(change)
, wherechange
is a dictionary. The change dictionary at least holds a ‘type’ key. *type
: the type of notification. Other keys may be passed depending on the value of ‘type’. In the case where type is ‘change’, we also have the following keys: *owner
: the HasTraits instance *old
: the old value of the modified trait attribute *new
: the new value of the modified trait attribute *name
: the name of the modified trait attribute.names (list, str, All) – If names is All, the handler will apply to all traits. If a list of str, handler will apply to all names in the list. If a str, the handler will apply just to that name.
type (str, All (default: 'change')) – The type of notification to filter by. If equal to All, then all notifications are passed to the observe handler.
- on_trait_change(handler: EventHandler | None = None, name: Sentinel | str | None = None, remove: bool = False) None
DEPRECATED: Setup a handler to be called when a trait changes.
This is used to setup dynamic notifications of trait changes.
Static handlers can be created by creating methods on a HasTraits subclass with the naming convention ‘_[traitname]_changed’. Thus, to create static handler for the trait ‘a’, create the method _a_changed(self, name, old, new) (fewer arguments can be used, see below).
If remove is True and handler is not specified, all change handlers for the specified name are uninstalled.
- Parameters:
handler (callable, None) – A callable that is called when a trait changes. Its signature can be handler(), handler(name), handler(name, new), handler(name, old, new), or handler(name, old, new, self).
name (list, str, None) – If None, the handler will apply to all traits. If a list of str, handler will apply to all names in the list. If a str, the handler will apply just to that name.
remove (bool) – If False (the default), then install the handler. If True then unintall it.
- parent
A trait whose value must be an instance of a specified class.
The value can also be an instance of a subclass of the specified class.
Subclasses can declare default classes by overriding the klass attribute
- pgid = None
- pid = None
- ports_cached = False
- async post_launch(**kwargs: Any) None
Perform any steps following the kernel process launch.
This method is called from KernelManager.post_start_kernel() as part of its start kernel sequence.
- async pre_launch(**kwargs: Any) Dict[str, Any]
Perform any steps in preparation for kernel process launch.
This includes applying additional substitutions to the kernel launch command and env. It also includes preparation of launch parameters.
Returns the updated kwargs.
- process = None
- async send_signal(signum: int) None
Sends a signal to the process group of the kernel (this usually includes the kernel and any subprocesses spawned by the kernel).
Note that since only SIGTERM is supported on Windows, we will check if the desired signal is for interrupt and apply the applicable code on Windows in that case.
- set_trait(name: str, value: Any) None
Forcibly sets trait attribute, including read-only attributes.
- async shutdown_requested(restart: bool = False) None
Allows the provisioner to determine if the kernel’s shutdown has been requested.
This method is called from KernelManager.request_shutdown() as part of its shutdown sequence.
This method is optional and is primarily used in scenarios where the provisioner may need to perform other operations in preparation for a kernel’s shutdown.
- trait_defaults(*names: str, **metadata: Any) dict[str, Any] | Sentinel
Return a trait’s default value or a dictionary of them
Notes
Dynamically generated default values may depend on the current state of the object.
- classmethod trait_events(name: str | None = None) dict[str, EventHandler]
Get a
dict
of all the event handlers of this class.- Parameters:
name (str (default: None)) – The name of a trait of this class. If name is
None
then all the event handlers of this class will be returned instead.- Return type:
The event handlers associated with a trait name, or all event handlers.
- trait_has_value(name: str) bool
Returns True if the specified trait has a value.
This will return false even if
getattr
would return a dynamically generated default value. These default values will be recognized as existing only after they have been generated.Example
class MyClass(HasTraits): i = Int() mc = MyClass() assert not mc.trait_has_value("i") mc.i # generates a default value assert mc.trait_has_value("i")
- trait_metadata(traitname: str, key: str, default: Any = None) Any
Get metadata values for trait by key.
- trait_values(**metadata: Any) dict[str, Any]
A
dict
of trait names and their values.The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. If a metadata key doesn’t exist, None will be passed to the function.
- Return type:
A
dict
of trait names and their values.
Notes
Trait values are retrieved via
getattr
, any exceptions raised by traits or the operations they may trigger will result in the absence of a trait value in the resultdict
.
- traits(**metadata: Any) dict[str, TraitType[Any, Any]]
Get a
dict
of all the traits of this class. The dictionary is keyed on the name and the values are the TraitType objects.The TraitTypes returned don’t know anything about the values that the various HasTrait’s instances are holding.
The metadata kwargs allow functions to be passed in which filter traits based on metadata values. The functions should take a single value as an argument and return a boolean. If any function returns False, then the trait is not included in the output. If a metadata key doesn’t exist, None will be passed to the function.
- unobserve(handler: Callable[[...], Any], names: Sentinel | str | Iterable[Sentinel | str] = traitlets.All, type: Sentinel | str = 'change') None
Remove a trait change handler.
This is used to unregister handlers to trait change notifications.
- Parameters:
handler (callable) – The callable called when a trait attribute changes.
names (list, str, All (default: All)) – The names of the traits for which the specified handler should be uninstalled. If names is All, the specified handler is uninstalled from the list of notifiers corresponding to all changes.
type (str or All (default: 'change')) – The type of notification to filter by. If All, the specified handler is uninstalled from the list of notifiers corresponding to all types.