MCPcopy Create free account
hub / github.com/cloudpipe/cloudpickle / run_in_notebook

Function run_in_notebook

tests/cloudpickle_ipykernel_test.py:19–53  ·  view source on GitHub ↗
(code, timeout=10)

Source from the content-addressed store, hash-verified

17
18
19def run_in_notebook(code, timeout=10):
20
21 km = ipykernel.connect.jupyter_client.KernelManager()
22 km.start_kernel()
23 kc = km.client()
24 kc.start_channels()
25 status, output, err = "kernel_started", None, None
26 try:
27 assert km.is_alive() and kc.is_alive()
28 kc.wait_for_ready()
29 idx = kc.execute(code)
30 running = True
31 while running:
32 try:
33 res = kc.iopub_channel.get_msg(timeout=timeout)
34 except Empty:
35 status = "timeout"
36 break
37 if res['parent_header'].get('msg_id') != idx:
38 continue
39 content = res['content']
40 if content.get("name", "state") == "stdout":
41 output = content['text']
42 if "traceback" in content:
43 err = "\n".join(content['traceback'])
44 status = "error"
45 running = res['content'].get('execution_state', None) != "idle"
46 finally:
47 kc.shutdown()
48 kc.stop_channels()
49 km.shutdown_kernel(now=True, restart=False)
50 assert not km.is_alive()
51 if status not in ["error", "timeout"]:
52 status = "ok" if not running else "exec_error"
53 return status, output, err
54
55
56@pytest.mark.skipif(

Calls 1

joinMethod · 0.80

Tested by

no test coverage detected