2024-08-17 00:18:57 +08:00
|
|
|
|
import{_ as e,r as t,o,c as p,b as n,d as s,a as c,e as i}from"./app-D1iI42LG.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><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>
|
2024-08-12 05:51:00 +08:00
|
|
|
|
<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
|
2024-08-12 06:07:50 +08:00
|
|
|
|
<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">"nonebot-passive"</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">"nonebot-active"</span><span class="token punctuation">)</span> <span class="token comment"># 获取主动通道</span>
|
2024-08-12 05:51:00 +08:00
|
|
|
|
liteyuki_bot <span class="token operator">=</span> get_bot<span class="token punctuation">(</span><span class="token punctuation">)</span>
|
|
|
|
|
|
2024-08-12 06:07:50 +08:00
|
|
|
|
<span class="token comment"># 注册一个函数在轻雪启动后运行</span>
|
2024-08-12 05:51:00 +08:00
|
|
|
|
<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">"I am liteyuki main process passive"</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">"I am liteyuki main process active"</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>
|
2024-08-12 06:07:50 +08:00
|
|
|
|
</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
|
2024-08-12 05:51:00 +08:00
|
|
|
|
<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>
|
|
|
|
|
|
2024-08-12 06:07:50 +08:00
|
|
|
|
<span class="token comment"># 通过get_channel函数获取通道对象,参数为调用set_channel时的通道标识</span>
|
2024-08-12 05:51:00 +08:00
|
|
|
|
channel_passive <span class="token operator">=</span> get_channel<span class="token punctuation">(</span><span class="token string">"nonebot-passive"</span><span class="token punctuation">)</span>
|
|
|
|
|
channel_active <span class="token operator">=</span> get_channel<span class="token punctuation">(</span><span class="token string">"nonebot-active"</span><span class="token punctuation">)</span>
|
|
|
|
|
|
2024-08-12 06:07:50 +08:00
|
|
|
|
<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>
|
2024-08-12 05:51:00 +08:00
|
|
|
|
<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"Passive receive: </span><span class="token interpolation"><span class="token punctuation">{</span>data<span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span>
|
|
|
|
|
|
2024-08-12 06:07:50 +08:00
|
|
|
|
|
|
|
|
|
<span class="token comment"># 注册一个函数在NoneBot启动后运行</span>
|
2024-08-12 05:51:00 +08:00
|
|
|
|
<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"Active receive: </span><span class="token interpolation"><span class="token punctuation">{</span>data<span class="token punctuation">}</span></span><span class="token string">"</span></span><span class="token punctuation">)</span>
|
2024-08-12 06:07:50 +08:00
|
|
|
|
</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
|
2024-08-12 05:51:00 +08:00
|
|
|
|
<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>.
|
2024-08-17 00:18:57 +08:00
|
|
|
|
</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><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">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">"key"</span><span class="token punctuation">,</span> <span class="token string">"value"</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">"key"</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("ul",null,[n("li",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":[["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:updated_time","content":"2024-08-16T16:18:06.000Z"}],["meta",{"property":"article:author","content":"远野千束"}],["meta",{"property":"article:modified_time","content":"2024-08-16T16:18:06.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"进程通信\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-16T16:18:06.000Z\\",\\"author\\":[{\\"@type\\":\\"Person\\",\\"name\\":\\"远野千束\\",\\"url\\":\\"https://sfkm.me\\"}]}"]]},"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":1723825086000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":3}]},"readingTime":{"minutes":2.74,"words":821},"filePathRelative":"dev/dev_comm.md","localizedDate":"2024年8月11日","autoDesc":true}');export{h as comp,b as data};
|