将来自其他语言的消息发送到IPython内核 [英] Sending messages from other languages to an IPython kernel

查看:210
本文介绍了将来自其他语言的消息发送到IPython内核的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有从Python以外与IPython内核通信的经验?

Does anyone have any experience of communicating with IPython kernels from outside of Python?

如果我试图将消息从Python应用程序发送到IPython内核,我使用 zmq.kernelmanager API。事实上,我显然需要用另一种语言编写自己的内核管理器,但是我找不到我正在寻找的有关低级消息传递协议的信息。

If I were trying to send messages from a Python app to an IPython kernel, I'd use the zmq.kernelmanager API. As it is, I'll obviously need to write my own kernel manager in another language, but I can't find the information that I'm looking for about the low-level messaging protocols.

是否有官方规范或备忘单记录了通过0MQ发送的实际消息的结构? 此页面描述的是比我正在寻找的更高级别的协议...我会吗?必须手动拉开实现以找到我想要的东西?

Is there an official spec or a 'cheat sheet' that documents the structure of the actual messages that get sent over 0MQ? This page describes a higher-level protocol than what I'm looking for... Will I have to manually pull apart the implementation to find what I want?

推荐答案

这是一个绝望需要存在的文件,但是有线协议的实现是在单个对象中实现的,所以从那里开始不应该太难。您链接的消息规范文档涵盖了每个字段的应用程序级内容,但不是它如何通过zeromq实际序列化。假设您有一个消息,如该文档中所述,有线格式非常简单。它是至少六个部分的多部分zeromq消息:

This is a document that desparately needs to exist, but the implementation of the wire protocol is implemented in a single object, so it shouldn't be too hard to grok from there. The message spec doc you linked covers the application-level content of each field, but not how it's actually serialized over zeromq. Assuming you have a mesage, as described in that doc, the wire format is pretty simple. It is a multipart zeromq message of at least six parts:


  • 主要消息部分是zeromq路由标识(零到多)

  • 后面跟着一个分隔符消息,字节< IDS | MSG>

  • 消息的hmac 摘要(如果禁用身份验证,则为空字符串''

  • 标题

  • parent_header

  • 元数据

  • content

  • The leading message parts are the zeromq routing identities (zero-to-many)
  • These are followed by a delimiter message, with the bytes <IDS|MSG>
  • The hmac digest of the message (an empty string '' if authentication is disabled)
  • header
  • parent_header
  • metadata
  • content

header parent_header ,<$消息传递文档中描述了c $ c> metadata 和 content - 这些是字典,并且序列化为字节,当前使用的是任何序列化。 IPython中的默认值是utf8编码的JSON,但允许任意序列化(msgpack是最常见的非默认值)。尚未在文档中描述的是用于身份验证的摘要。这是消息的 MD5 HMAC Digest 。摘要的密钥位于连接文件的 key 字段中。 HMAC摘要使用的消息是序列化标题,parent_header,元数据和内容的字节串联,按照通过网络发送的相同顺序。

header, parent_header, metadata, and content are all described in the messaging doc - these are dictionaries, and serialized to bytes with whatever serialization is currently used. The default in IPython is utf8-encoded JSON, but arbitrary serialization is allowed (msgpack being the most common non-default). Not yet described in the docs is the digest, used for authentication. This is an MD5 HMAC Digest of the message. The key for the digest is found in the key field of the connection file. The 'message' used by the HMAC digest is the concatenation of the bytes of the serialized header, parent_header, metadata, and content, in the same order sent over the wire.

您可以通过指定配置值来禁用邮件签名

You can disable message signing by specifying the config value

Session.key = ''

到代码的IPython相关部分,在这种情况下,摘要字段将始终为空字符串'' 。我建议你在开始时这样做,这样你就可以先了解实现中更有趣的部分。

to the IPython-related parts of your code, in which case the digest field will always be an empty string ''. I would recommend doing this while getting started, so you can work out the more interesting parts of the implementation first.

这是一个示例执行请求,它的回复实际上是由IPython。

Here are a sample execute request and its reply actually sent by IPython.

请求:

[
  <IDS|MSG>
  6ea6b213262402cc1ad3c1d3e342a9f6
  {"date":"2013-04-27T23:22:13.522049","username":"minrk","session":"5b03b89a-93c9-4113-bb85-17ba57233711","msg_id":"c6d0f85e-fc25-4f1e-84e1-3d706b615393","msg_type":"execute_request"}
  {}
  {}
  {"user_variables":[],"code":"1\n","silent":false,"allow_stdin":true,"store_history":true,"user_expressions":{}}
]

及其回复:

[
  5b03b89a-93c9-4113-bb85-17ba57233711
  <IDS|MSG>
  47d1052f6e8f333d18480938ca91719b
  {"date":"2013-04-27T23:22:13.528239","username":"kernel","session":"d7eb303b-d2d0-4723-aef2-738545a8da11","msg_id":"9ed1d332-398c-4132-b203-1e7bf8fed712","msg_type":"execute_reply"}
  {"date":"2013-04-27T23:22:13.522049","username":"minrk","session":"5b03b89a-93c9-4113-bb85-17ba57233711","msg_id":"c6d0f85e-fc25-4f1e-84e1-3d706b615393","msg_type":"execute_request"}
  {"dependencies_met":true,"engine":"645fb29f-37ab-40c9-bc01-b7fbfe3c2112","status":"ok","started":"2013-04-27T23:22:13.524114"}
  {"status":"ok","execution_count":2,"user_variables":{},"payload":[],"user_expressions":{}}
]

这篇关于将来自其他语言的消息发送到IPython内核的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆