Wolfram 语言

通道框架

处理任何类型的数据

逐字给出以 JSON 格式(字符串、数字、TrueFalseNull)、列表、规则或包含这些元素的关联表示的消息.

In[1]:=
Click for copyable input
listener = ChannelListen["example"]
Out[1]=
In[2]:=
Click for copyable input
(ChannelSend["example", #]) & /@ {"string", 100, True, {1, 2, 3}, "a" -> "b", {"c" -> 1}, <|"d" -> True|>};
In[3]:=
Click for copyable input
listener["Message", All]
Out[3]=

包含消息的代码被安全封装在 HoldComplete,使你可以在运行之前进行检验.

In[4]:=
Click for copyable input
ChannelSend["example", Unevaluated[Print["hello"]]];
In[5]:=
Click for copyable input
listener["Message"]
Out[5]=
In[6]:=
Click for copyable input
listener["Message"] // ReleaseHold

创建一个发送压缩后的笔记本,然后在新窗口打开收到的笔记本的通道.

In[7]:=
Click for copyable input
CreateChannel["nb", ChannelPreSendFunction -> (Compress[#1] &), HandlerFunctions -> Association[ "MessageReceived" -> (CreateWindow[Uncompress[#Message]] &)]]
Out[7]=

开始在信道侦听.

In[8]:=
Click for copyable input
listener = ChannelListen["nb"]
Out[8]=

创建一个笔记本并将其发送至信道.

In[9]:=
Click for copyable input
nb = DocumentNotebook[{TextCell["My Report", "Section"], TextCell["Lorem ipsum dolor sit amet:", "Text"], ExpressionCell[ ContourPlot[x + Sin[x^2 + y^2], {x, -4, 4}, {y, -4, 4}, Contours -> 9, ContourShading -> ColorData[35, "ColorList"]]]}, StyleDefinitions -> FrontEnd`FileName[{"Article"}, "Preprint.nb"], WindowTitle -> "My Report", Background -> LightOrange]
Out[9]=
In[10]:=
Click for copyable input
ChannelSend["nb", nb]
Out[10]=

收到的笔记本.

Out[11]=

相关范例

de en es fr ja ko pt-br ru