1
0
forked from bot/app
LiteyukiBot/assets/dev_comm.html-SU42FWy3.js

48 lines
15 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import{_ as e,r as t,o,c as p,b as n,d as s,a as c,e as i}from"./app-BiKNfxRB.js";const l={},u=i(`<h2 id="通道通信" tabindex="-1"><a class="header-anchor" href="#通道通信"><span><strong>通道通信</strong></span></a></h2><h3 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h3><p>轻雪运行在主进程 MainProcess 里,其他插件框架进程是伴随的子进程,因此无法通过内存共享和直接对象传递的方式进行通信,轻雪提供了一个通道<code>Channel</code>用于跨进程通信,你可以通过<code>Channel</code>发送消息给其他进程,也可以监听其他进程的消息。</p><p>例如子进程接收到用户信息需要重启机器人,这时可以通过通道对主进程发送消息,主进程接收到消息后重启对应子进程。</p><h3 id="示例" tabindex="-1"><a class="header-anchor" href="#示例"><span>示例</span></a></h3><p>通道是全双工的,有两种接收模式,但一个通道只能使用一种,即被动模式和主动模式,被动模式由<code>chan.on_receive()</code>装饰回调函数实现,主动模式需调用<code>chan.receive()</code>实现</p><ul><li>创建子进程的同时会初始化一个被动通道和一个主动通道,且通道标识为<code>{process_name}-active</code>和<code>{process_name}-passive</code></li><li>主进程中通过<code>get_channel</code>函数获取通道对象</li><li>子进程中导入单例<code>active_channel</code>及<code>passive_channel</code>即可</li></ul><blockquote><p>在轻雪插件中(主进程中)</p></blockquote><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>
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">send_data</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>
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> <span class="token comment"># 每3秒发送一次消息</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></div><blockquote><p>在子进程中例如NoneBot插件中</p></blockquote><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> active_channel<span class="token punctuation">,</span> passive_channel <span class="token comment"># 子进程中获取通道直接导入进程全局单例即可</span>
<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"># 被动模式,通过装饰器注册一个函数在接收到消息时运行,每次接收到字符串数据时都会运行</span>
<span class="token decorator annotation punctuation">@passive_channel<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">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> active_channel<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></div><blockquote><p>启动后控制台输出</p></blockquote><div class="language-log line-numbers-mode" data-ext="log" data-title="log"><pre class="language-log"><code><span class="token date number">0000-00-00</span> <span class="token time number">00:00:00</span> <span class="token punctuation">[</span>ℹ️信息<span class="token punctuation">]</span> <span class="token property">Passive receive:</span> I am liteyuki main process passive
<span class="token date number">0000-00-00</span> <span class="token time number">00:00:00</span> <span class="token punctuation">[</span>ℹ️信息<span class="token punctuation">]</span> <span class="token property">Active receive:</span> I am liteyuki main process active
<span class="token date number">0000-00-00</span> <span class="token time number">00:00:03</span> <span class="token punctuation">[</span>ℹ️信息<span class="token punctuation">]</span> <span class="token property">Passive receive:</span> I am liteyuki main process passive
<span class="token date number">0000-00-00</span> <span class="token time number">00:00:03</span> <span class="token punctuation">[</span>ℹ️信息<span class="token punctuation">]</span> <span class="token property">Active receive:</span> I am liteyuki main process active
<span class="token punctuation">.</span><span class="token punctuation">.</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></div><h2 id="共享内存通信" tabindex="-1"><a class="header-anchor" href="#共享内存通信"><span><strong>共享内存通信</strong></span></a></h2><h3 id="简介-1" tabindex="-1"><a class="header-anchor" href="#简介-1"><span>简介</span></a></h3><ul><li>相比于普通进程通信,内存共享使得代码编写更加简洁,轻雪框架提供了一个内存共享通信的接口,你可以通过<code>storage</code>模块实现内存共享通信,该模块封装通道实现</li><li>内存共享是线程安全的,你可以在多个线程中读写共享内存,线程锁会自动保护共享内存的读写操作</li></ul><h3 id="示例-1" tabindex="-1"><a class="header-anchor" href="#示例-1"><span>示例</span></a></h3><blockquote><p>在任意进程中均可使用</p></blockquote><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">from</span> liteyuki<span class="token punctuation">.</span>comm<span class="token punctuation">.</span>storage <span class="token keyword">import</span> shared_memory
shared_memory<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span><span class="token string">&quot;key&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;value&quot;</span><span class="token punctuation">)</span> <span class="token comment"># 设置共享内存</span>
value <span class="token operator">=</span> shared_memory<span class="token punctuation">.</span>get<span class="token punctuation">(</span><span class="token string">&quot;key&quot;</span><span class="token punctuation">)</span> <span class="token comment"># 获取共享内存</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></div>`,19),r={href:"https://github.com/LiteyukiStudio/LiteyukiBot/blob/main/liteyuki/comm/storage.py",target:"_blank",rel:"noopener noreferrer"};function d(k,m){const a=t("ExternalLinkIcon");return o(),p("div",null,[u,n("p",null,[s("源代码:"),n("a",r,[s("liteyuki/comm/storage.py"),c(a)])])])}const h=e(l,[["render",d],["__file","dev_comm.html.vue"]]),b=JSON.parse('{"path":"/dev/dev_comm.html","title":"进程通信","lang":"zh-CN","frontmatter":{"title":"进程通信","icon":"exchange-alt","order":4,"category":"开发","description":"通道通信 简介 轻雪运行在主进程 MainProcess 里其他插件框架进程是伴随的子进程因此无法通过内存共享和直接对象传递的方式进行通信轻雪提供了一个通道Channel用于跨进程通信你可以通过Channel发送消息给其他进程也可以监听其他进程的消息。 例如子进程接收到用户信息需要重启机器人,这时可以通过通道对主进程发送消息,主进程接收到消息...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/dev_comm.html"}],["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 里其他插件框架进程是伴随的子进程因此无法通过内存共享和直接对象传递的方式进行通信轻雪提供了一个通道Channel用于跨进程通信你可以通过Channel发送消息给其他进程也可以监听其他进程的消息。 例如子进程接收到用户信息需要重启机器人,这时可以通过通道对主进程发送消息,主进程接收到消息..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:locale:alternate","content":"en-US"}],["meta",{"property":"og:updated_time","content":"2024-08-29T06:23:13.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-29T06:23:13.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"进程通信\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-29T06:23:13.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":2,"title":"通道通信","slug":"通道通信","link":"#通道通信","children":[{"level":3,"title":"简介","slug":"简介","link":"#简介","children":[]},{"level":3,"title":"示例","slug":"示例","link":"#示例","children":[]}]},{"level":2,"title":"共享内存通信","slug":"共享内存通信","link":"#共享内存通信","children":[{"level":3,"title":"简介","slug":"简介-1","link":"#简介-1","children":[]},{"level":3,"title":"示例","slug":"示例-1","link":"#示例-1","children":[]}]}],"git":{"createdTime":1723413012000,"updatedTime":1724912593000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":6},{"name":"Snowykami","email":"snowykami@outlook.com","commits":1}]},"readingTime":{"minutes":2.52,"words":757},"filePathRelative":"dev/dev_comm.md","localizedDate":"2024年8月11日","autoDesc":true}');export{h as comp,b as data};