MCPcopy Index your code
hub / github.com/zai-org/GLM-4 / CodeKernel

Class CodeKernel

demo/composite_demo/src/tools/python.py:18–145  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16
17
18class CodeKernel:
19 def __init__(
20 self,
21 kernel_name="kernel",
22 kernel_id=None,
23 kernel_config_path="",
24 python_path=None,
25 ipython_path=None,
26 init_file_path="./startup.py",
27 verbose=1,
28 ):
29 self.kernel_name = kernel_name
30 self.kernel_id = kernel_id
31 self.kernel_config_path = kernel_config_path
32 self.python_path = python_path
33 self.ipython_path = ipython_path
34 self.init_file_path = init_file_path
35 self.verbose = verbose
36
37 if python_path is None and ipython_path is None:
38 env = None
39 else:
40 env = {"PATH": self.python_path + ":$PATH", "PYTHONPATH": self.python_path}
41
42 # Initialize the backend kernel
43 self.kernel_manager = jupyter_client.KernelManager(
44 kernel_name=IPYKERNEL, connection_file=self.kernel_config_path, exec_files=[self.init_file_path], env=env
45 )
46 if self.kernel_config_path:
47 self.kernel_manager.load_connection_file()
48 self.kernel_manager.start_kernel(stdout=PIPE, stderr=PIPE)
49 print("Backend kernel started with the configuration: {}".format(self.kernel_config_path))
50 else:
51 self.kernel_manager.start_kernel(stdout=PIPE, stderr=PIPE)
52 print("Backend kernel started with the configuration: {}".format(self.kernel_manager.connection_file))
53
54 if verbose:
55 pprint(self.kernel_manager.get_connection_info())
56
57 # Initialize the code kernel
58 self.kernel = self.kernel_manager.blocking_client()
59 # self.kernel.load_connection_file()
60 self.kernel.start_channels()
61 print("Code kernel started.")
62
63 def execute(self, code):
64 self.kernel.execute(code)
65 try:
66 shell_msg = self.kernel.get_shell_msg(timeout=30)
67 io_msg_content = self.kernel.get_iopub_msg(timeout=30)["content"]
68 while True:
69 msg_out = io_msg_content
70 ### Poll the message
71 try:
72 io_msg_content = self.kernel.get_iopub_msg(timeout=30)["content"]
73 if "execution_state" in io_msg_content and io_msg_content["execution_state"] == "idle":
74 break
75 except queue.Empty:

Callers 1

get_kernelFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected