app/assets/dev_comm.html-B7Un-Jum.js

49 lines
14 KiB
JavaScript
Raw Normal View History

import{_ as n,o as s,c as a,e}from"./app-CsbHEmsP.js";const t={},p=e(`<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2><p>轻雪运行在主进程 MainProcess 里,其他插件框架进程是伴随的子进程,因此无法通过内存共享和直接对象传递的方式进行通信,轻雪提供了一个通道<code>Channel</code>用于跨进程通信,你可以通过<code>Channel</code>发送消息给其他进程,也可以监听其他进程的消息。</p><p>例如子进程接收到用户信息需要重启机器人,这时可以通过通道对主进程发送消息,主进程接收到消息后重启对应子进程。</p><h2 id="快速开始" tabindex="-1"><a class="header-anchor" href="#快速开始"><span>快速开始</span></a></h2><p>通道是全双工的,有两种接收模式,但一个通道只能使用一种,即被动模式和主动模式,被动模式由<code>chan.on_receive()</code>装饰回调函数实现,主动模式需调用<code>chan.receive()</code>实现</p><p>轻雪核心在创建子进程的同时会初始化两个通道,一个被动通道和一个主动通道,且通道标识为<code>{process_name}-active</code>和<code>{process_name}-passive</code>,你可以通过<code>get_channel</code>函数获取通道对象,然后通过<code>send</code>方法发送消息</p><ul><li>在轻雪插件中</li></ul><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code>
<span class="token keyword">import</span> asyncio
<span class="token keyword">from</span> liteyuki<span class="token punctuation">.</span>comm <span class="token keyword">import</span> get_channel<span class="token punctuation">,</span> Channel
<span class="token keyword">from</span> liteyuki <span class="token keyword">import</span> get_bot
<span class="token comment"># get_channel函数获取通道对象参数为调用set_channel时的通道标识</span>
<span class="token comment"># 每个进程只能获取在当前进程通过set_channel设置的通道</span>
<span class="token comment"># 轻雪已经在创建进程时把每个通道都传递给了主进程和子进程以便在哪个进程都可以被获取</span>
channel_passive <span class="token operator">=</span> get_channel<span class="token punctuation">(</span><span class="token string">&quot;nonebot-passive&quot;</span><span class="token punctuation">)</span> <span class="token comment"># </span>
channel_active <span class="token operator">=</span> get_channel<span class="token punctuation">(</span><span class="token string">&quot;nonebot-active&quot;</span><span class="token punctuation">)</span> <span class="token comment"># </span>
liteyuki_bot <span class="token operator">=</span> get_bot<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 注册一个函数在轻雪启动后运行</span>
<span class="token decorator annotation punctuation">@liteyuki_bot<span class="token punctuation">.</span>on_after_start</span>
<span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">do_something_after_start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
channel_passive<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token string">&quot;I am liteyuki main process passive&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">await</span> asyncio<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">0.1</span><span class="token punctuation">)</span>
channel_active<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token string">&quot;I am liteyuki main process active&quot;</span><span class="token punctuation">)</span>
<span class="token keyword">await</span> asyncio<span class="token punctuation">.</span>sleep<span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><ul><li>NoneBot</li></ul><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">from</span> nonebot <span class="token keyword">import</span> get_driver
<span class="token keyword">from</span> liteyuki<span class="token punctuation">.</span>comm <span class="token keyword">import</span> get_channel
<span class="token keyword">from</span> liteyuki<span class="token punctuation">.</span>log <span class="token keyword">import</span> logger
driver <span class="token operator">=</span> get_driver<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token comment"># 通过get_channel函数获取通道对象参数为调用set_channel时的通道标识</span>
channel_passive <span class="token operator">=</span> get_channel<span class="token punctuation">(</span><span class="token string">&quot;nonebot-passive&quot;</span><span class="token punctuation">)</span>
channel_active <span class="token operator">=</span> get_channel<span class="token punctuation">(</span><span class="token string">&quot;nonebot-active&quot;</span><span class="token punctuation">)</span>
<span class="token comment"># 被动模式通过装饰器注册一个函数在接收到消息时运行每次接收到字符串数据时都会运行</span>
<span class="token decorator annotation punctuation">@channel_passive<span class="token punctuation">.</span>on_receive</span><span class="token punctuation">(</span>filter_func<span class="token operator">=</span><span class="token keyword">lambda</span> data<span class="token punctuation">:</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token builtin">str</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">on_passive_receive</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">:</span>
logger<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&quot;Passive receive: </span><span class="token interpolation"><span class="token punctuation">{</span>data<span class="token punctuation">}</span></span><span class="token string">&quot;</span></span><span class="token punctuation">)</span>
<span class="token comment"># 注册一个函数在NoneBot启动后运行</span>
<span class="token decorator annotation punctuation">@driver<span class="token punctuation">.</span>on_startup</span>
<span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">on_startup</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">while</span> <span class="token boolean">True</span><span class="token punctuation">:</span>
data <span class="token operator">=</span> channel_active<span class="token punctuation">.</span>receive<span class="token punctuation">(</span><span class="token punctuation">)</span>
logger<span class="token punctuation">.</span>info<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&quot;Active receive: </span><span class="token interpolation"><span class="token punctuation">{</span>data<span class="token punctuation">}</span></span><span class="token string">&quot;</span></span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><ul><li></li></ul><div class="language-bash line-numbers-mode" data-ext="sh" data-title="sh"><pre class="language-bash"><code><span class="token punctuation">..</span>-<span class="token punctuation">..</span>-<span class="token punctuation">..</span> <span class="token punctuation">..</span>:<span class="token punctuation">..</span>:<span class="token punctuation">..</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> Passive receive: I am liteyuki main process passive
<span class="token punctuation">..</span>-<span class="token punctuation">..</span>-<span class="token punctuation">..</span> <span class="token punctuation">..</span>:<span class="token punctuation">..</span>:<span class="token punctuation">..</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> Active receive: I am liteyuki main process active
<span class="token punctuation">..</span>-<span class="token punctuation">..</span>-<span class="token punctuation">..</span> <span class="token punctuation">..</span>:<span class="token punctuation">..</span>:<span class="token punctuation">..</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> Passive receive: I am liteyuki main process passive
<span class="token punctuation">..</span>-<span class="token punctuation">..</span>-<span class="token punctuation">..</span> <span class="token punctuation">..</span>:<span class="token punctuation">..</span>:<span class="token punctuation">..</span> <span class="token punctuation">[</span><span class="token punctuation">]</span> Active receive: I am liteyuki main process active
<span class="token punctuation">..</span>.
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div>`,12),o=[p];function c(i,l){return s(),a("div",null,o)}const r=n(t,[["render",c],["__file","dev_comm.html.vue"]]),d=JSON.parse('{"path":"/dev/dev_comm.html","title":"","lang":"zh-CN","frontmatter":{"title":"","icon":"exchange-alt","order":4,"category":"","description":" MainProcess ChannelChannel ...","head":[["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/dev_comm.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot "}],["meta",{"property":"og:title","content":""}],["meta",{"property":"og:description","content":" MainProcess ChannelChannel ..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:updated_time","content":"2024-08-11T22:07:01.000Z"}],["meta",{"property":"article:author","content":""}],["meta",{"property":"article:modified_time","content":"2024-08-11T22:07:01.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-11T22:07:01.000Z\\",\\"author\\":[{\\"@type\\":\\"Person\\",\\"name\\":\\"\\",\\"url\\":\\"https://sfkm.me\\"}]}"]]},"headers":[{"level":2,"title":"","slug":"","link":"#","children":[]},{"level":2,"title":"","slug":"","link":"#","children":[]}],"git":{"createdTime":1723413012000,"updatedTime":1723414021000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":2}]},"readingTime":{"minutes":2.22,"words":667},"filePathRelative":"dev/dev_comm.md","localizedDate":"2024811","autoDesc":true}');export{r as comp,d as data};