我有一个现有的 ipython 内核,带有一个通信文件“path/comm_file.json”,我想使用内核客户端 API 执行该内核中的代码(实际上我并不挑剔,任何方法都可以..)。我明白这是从 jupyter 做事情的最好方法文档 https://jupyter-client.readthedocs.org/en/latest/api/client.html。所以我写了下面的代码:
from jupyter_client import KernelClient
client = KernelClient(connection_file='path/comm_file.json')
client.execute('a = 10')
但是执行方法会出现以下错误:
File "C:\Python27\lib\site-packages\jupyter_client\client.py", line 249, in execute
self.shell_channel.send(msg)
File "C:\Python27\lib\site-packages\jupyter_client\client.py", line 143, in shell_channel
socket, self.session, self.ioloop
TypeError: object.__new__() takes no parameters
我在这里做错了什么?
我也在试图弄清楚客户端是如何工作的。这是一个开始的地方:
对于简单的阻塞客户端,您可以看看如何jupyter_测试_客户端 https://github.com/jupyter/jupyter_kernel_test and jupyter_console https://github.com/jupyter/jupyter_console作品。
from pprint import pprint
from jupyter_client.consoleapp import JupyterConsoleApp
class MyKernelApp(JupyterConsoleApp):
def __init__(self, connection_file, runtime_dir):
self._dispatching = False
self.existing = connection_file
self.runtime_dir = runtime_dir
self.initialize()
app = MyKernelApp("connection.json", "/tmp")
kc = app.kernel_client
kc.execute("print 'hello'")
msg = kc.iopub_channel.get_msg(block=True, timeout=1)
pprint(msg)
您将需要辅助函数来正确处理 zmq 通道和 json 消息。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)