app/assets/channel.html-DxG1UVtC.js

213 lines
67 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 n,o as s,c as a,e as t}from"./app-BvUYPzLF.js";const e={},p=t(`<h1 id="liteyuki-comm-channel" tabindex="-1"><a class="header-anchor" href="#liteyuki-comm-channel"><span>liteyuki.comm.channel</span></a></h1><p><strong>Description</strong>: Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved</p><p>@Time : 2024/7/26 下午11:21 @Author : snowykami @Email : snowykami@outlook.com @File : channel_.py @Software: PyCharm</p><p>本模块定义了一个通用的通道类,用于进程间通信</p><h3 id="class-channel-generic-t" tabindex="-1"><a class="header-anchor" href="#class-channel-generic-t"><span><em><strong>class</strong></em> <code>Channel(Generic[T])</code></span></a></h3><h4 id="def-init-self-name-str-type-check-optional-bool-none" tabindex="-1"><a class="header-anchor" href="#def-init-self-name-str-type-check-optional-bool-none"><span><em><strong>def</strong></em> <code>__init__(self, name: str, type_check: Optional[bool] = None)</code></span></a></h4><p><strong>Description</strong>: 初始化通道</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>name: 通道ID</li><li>type_check: 是否开启类型检查, 若为空,则传入泛型默认开启,否则默认关闭</li></ul></blockquote><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L41" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> name<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> type_check<span class="token punctuation">:</span> Optional<span class="token punctuation">[</span><span class="token builtin">bool</span><span class="token punctuation">]</span><span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
初始化通道
Args:
name: 通道ID
type_check: 是否开启类型检查, 若为空,则传入泛型默认开启,否则默认关闭
&quot;&quot;&quot;</span>
<span class="token punctuation">(</span>self<span class="token punctuation">.</span>conn_send<span class="token punctuation">,</span> self<span class="token punctuation">.</span>conn_recv<span class="token punctuation">)</span> <span class="token operator">=</span> Pipe<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">(</span>self<span class="token punctuation">.</span>_conn_send_inner<span class="token punctuation">,</span> self<span class="token punctuation">.</span>_conn_recv_inner<span class="token punctuation">)</span> <span class="token operator">=</span> Pipe<span class="token punctuation">(</span><span class="token punctuation">)</span>
self<span class="token punctuation">.</span>_closed <span class="token operator">=</span> <span class="token boolean">False</span>
self<span class="token punctuation">.</span>_on_main_receive_func_ids<span class="token punctuation">:</span> <span class="token builtin">list</span><span class="token punctuation">[</span><span class="token builtin">int</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
self<span class="token punctuation">.</span>_on_sub_receive_func_ids<span class="token punctuation">:</span> <span class="token builtin">list</span><span class="token punctuation">[</span><span class="token builtin">int</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
self<span class="token punctuation">.</span>name<span class="token punctuation">:</span> <span class="token builtin">str</span> <span class="token operator">=</span> name
self<span class="token punctuation">.</span>is_receive_loop_running <span class="token operator">=</span> <span class="token boolean">False</span>
<span class="token keyword">if</span> type_check <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
type_check <span class="token operator">=</span> self<span class="token punctuation">.</span>_get_generic_type<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span>
<span class="token keyword">elif</span> type_check<span class="token punctuation">:</span>
<span class="token keyword">if</span> self<span class="token punctuation">.</span>_get_generic_type<span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
<span class="token keyword">raise</span> TypeError<span class="token punctuation">(</span><span class="token string">&#39;Type hint is required for enforcing type check.&#39;</span><span class="token punctuation">)</span>
self<span class="token punctuation">.</span>type_check <span class="token operator">=</span> type_check
<span class="token keyword">if</span> name <span class="token keyword">in</span> _channel<span class="token punctuation">:</span>
<span class="token keyword">raise</span> ValueError<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&#39;Channel </span><span class="token interpolation"><span class="token punctuation">{</span>name<span class="token punctuation">}</span></span><span class="token string"> already exists&#39;</span></span><span class="token punctuation">)</span>
<span class="token keyword">if</span> IS_MAIN_PROCESS<span class="token punctuation">:</span>
<span class="token keyword">if</span> name <span class="token keyword">in</span> _channel<span class="token punctuation">:</span>
<span class="token keyword">raise</span> ValueError<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&#39;Channel </span><span class="token interpolation"><span class="token punctuation">{</span>name<span class="token punctuation">}</span></span><span class="token string"> already exists&#39;</span></span><span class="token punctuation">)</span>
_channel<span class="token punctuation">[</span>name<span class="token punctuation">]</span> <span class="token operator">=</span> self
logger<span class="token punctuation">.</span>debug<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&#39;Channel </span><span class="token interpolation"><span class="token punctuation">{</span>name<span class="token punctuation">}</span></span><span class="token string"> initialized in main process&#39;</span></span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
logger<span class="token punctuation">.</span>debug<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&#39;Channel </span><span class="token interpolation"><span class="token punctuation">{</span>name<span class="token punctuation">}</span></span><span class="token string"> initialized in sub process, should manually set in main process&#39;</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 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></details><h4 id="def-get-generic-type-self-optional-type" tabindex="-1"><a class="header-anchor" href="#def-get-generic-type-self-optional-type"><span><em><strong>def</strong></em> <code>_get_generic_type(self) -&gt; Optional[type]</code></span></a></h4><p><strong>Description</strong>: 获取通道传递泛型类型</p><p><strong>Return</strong>: Optional[type]: 泛型类型</p><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L77" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">_get_generic_type</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Optional<span class="token punctuation">[</span><span class="token builtin">type</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
获取通道传递泛型类型
Returns:
Optional[type]: 泛型类型
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> <span class="token builtin">hasattr</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> <span class="token string">&#39;__orig_class__&#39;</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> get_args<span class="token punctuation">(</span>self<span class="token punctuation">.</span>__orig_class__<span class="token punctuation">)</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span>
<span class="token keyword">return</span> <span class="token boolean">None</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></div></details><h4 id="def-validate-structure-self-data-any-structure-type-bool" tabindex="-1"><a class="header-anchor" href="#def-validate-structure-self-data-any-structure-type-bool"><span><em><strong>def</strong></em> <code>_validate_structure(self, data: Any, structure: type) -&gt; bool</code></span></a></h4><p><strong>Description</strong>: 验证数据结构</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>data: 数据</li><li>structure: 结构</li></ul></blockquote><p><strong>Return</strong>: bool: 是否通过验证</p><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L88" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">_validate_structure</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> data<span class="token punctuation">:</span> Any<span class="token punctuation">,</span> structure<span class="token punctuation">:</span> <span class="token builtin">type</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token builtin">bool</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
验证数据结构
Args:
data: 数据
structure: 结构
Returns:
bool: 是否通过验证
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>structure<span class="token punctuation">,</span> <span class="token builtin">type</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> structure<span class="token punctuation">)</span>
<span class="token keyword">elif</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>structure<span class="token punctuation">,</span> <span class="token builtin">tuple</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token builtin">tuple</span><span class="token punctuation">)</span> <span class="token keyword">or</span> <span class="token builtin">len</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token builtin">len</span><span class="token punctuation">(</span>structure<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token boolean">False</span>
<span class="token keyword">return</span> <span class="token builtin">all</span><span class="token punctuation">(</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>_validate_structure<span class="token punctuation">(</span>d<span class="token punctuation">,</span> s<span class="token punctuation">)</span> <span class="token keyword">for</span> <span class="token punctuation">(</span>d<span class="token punctuation">,</span> s<span class="token punctuation">)</span> <span class="token keyword">in</span> <span class="token builtin">zip</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> structure<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">elif</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>structure<span class="token punctuation">,</span> <span class="token builtin">list</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token builtin">list</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token boolean">False</span>
<span class="token keyword">return</span> <span class="token builtin">all</span><span class="token punctuation">(</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>_validate_structure<span class="token punctuation">(</span>d<span class="token punctuation">,</span> structure<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">for</span> d <span class="token keyword">in</span> data<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">elif</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>structure<span class="token punctuation">,</span> <span class="token builtin">dict</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>data<span class="token punctuation">,</span> <span class="token builtin">dict</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token boolean">False</span>
<span class="token keyword">return</span> <span class="token builtin">all</span><span class="token punctuation">(</span><span class="token punctuation">(</span>k <span class="token keyword">in</span> data <span class="token keyword">and</span> self<span class="token punctuation">.</span>_validate_structure<span class="token punctuation">(</span>data<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">,</span> structure<span class="token punctuation">[</span>k<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token keyword">for</span> k <span class="token keyword">in</span> structure<span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> <span class="token boolean">False</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 class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></details><h4 id="def-send-self-data-t" tabindex="-1"><a class="header-anchor" href="#def-send-self-data-t"><span><em><strong>def</strong></em> <code>send(self, data: T)</code></span></a></h4><p><strong>Description</strong>: 发送数据,发送函数为同步函数,没有异步的必要</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>data: 数据</li></ul></blockquote><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L117" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">send</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> data<span class="token punctuation">:</span> T<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
发送数据,发送函数为同步函数,没有异步的必要
Args:
data: 数据
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> self<span class="token punctuation">.</span>type_check<span class="token punctuation">:</span>
_type <span class="token operator">=</span> self<span class="token punctuation">.</span>_get_generic_type<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> _type <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span> <span class="token keyword">and</span> <span class="token punctuation">(</span><span class="token keyword">not</span> self<span class="token punctuation">.</span>_validate_structure<span class="token punctuation">(</span>data<span class="token punctuation">,</span> _type<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">raise</span> TypeError<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&#39;Data must be an instance of </span><span class="token interpolation"><span class="token punctuation">{</span>_type<span class="token punctuation">}</span></span><span class="token string">, </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token builtin">type</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">}</span></span><span class="token string"> found&#39;</span></span><span class="token punctuation">)</span>
<span class="token keyword">if</span> self<span class="token punctuation">.</span>_closed<span class="token punctuation">:</span>
<span class="token keyword">raise</span> RuntimeError<span class="token punctuation">(</span><span class="token string">&#39;Cannot send to a closed channel_&#39;</span><span class="token punctuation">)</span>
self<span class="token punctuation">.</span>conn_send<span class="token punctuation">.</span>send<span class="token punctuation">(</span>data<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></div></details><h4 id="def-receive-self-t" tabindex="-1"><a class="header-anchor" href="#def-receive-self-t"><span><em><strong>def</strong></em> <code>receive(self) -&gt; T</code></span></a></h4><p><strong>Description</strong>: 同步接收数据,会阻塞线程</p><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L132" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">receive</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> T<span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
同步接收数据,会阻塞线程
Args:
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> self<span class="token punctuation">.</span>_closed<span class="token punctuation">:</span>
<span class="token keyword">raise</span> RuntimeError<span class="token punctuation">(</span><span class="token string">&#39;Cannot receive from a closed channel_&#39;</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> self<span class="token punctuation">.</span>conn_recv<span class="token punctuation">.</span>recv<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> data
</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></div></details><h4 id="async-def-async-receive-self-t" tabindex="-1"><a class="header-anchor" href="#async-def-async-receive-self-t"><span><em><strong>async def</strong></em> <code>async_receive(self) -&gt; T</code></span></a></h4><p><strong>Description</strong>: 异步接收数据,会挂起等待</p><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L144" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">async_receive</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> T<span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
异步接收数据,会挂起等待
&quot;&quot;&quot;</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;等待接收数据&#39;</span><span class="token punctuation">)</span>
loop <span class="token operator">=</span> asyncio<span class="token punctuation">.</span>get_running_loop<span class="token punctuation">(</span><span class="token punctuation">)</span>
data <span class="token operator">=</span> <span class="token keyword">await</span> loop<span class="token punctuation">.</span>run_in_executor<span class="token punctuation">(</span><span class="token boolean">None</span><span class="token punctuation">,</span> self<span class="token punctuation">.</span>receive<span class="token punctuation">)</span>
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">&#39;接收到数据&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> data
</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></div></details><h4 id="def-on-receive-self-filter-func-optional-filter-func-none-callable-callable-t-any-callable-t-any" tabindex="-1"><a class="header-anchor" href="#def-on-receive-self-filter-func-optional-filter-func-none-callable-callable-t-any-callable-t-any"><span><em><strong>def</strong></em> <code>on_receive(self, filter_func: Optional[FILTER_FUNC] = None) -&gt; Callable[[Callable[[T], Any]], Callable[[T], Any]]</code></span></a></h4><p><strong>Description</strong>: 接收数据并执行函数</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>filter_func: 过滤函数为None则不过滤</li></ul></blockquote><p><strong>Return</strong>: 装饰器,装饰一个函数在接收到数据后执行</p><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L154" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">on_receive</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> filter_func<span class="token punctuation">:</span> Optional<span class="token punctuation">[</span>FILTER_FUNC<span class="token punctuation">]</span><span class="token operator">=</span><span class="token boolean">None</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Callable<span class="token punctuation">[</span><span class="token punctuation">[</span>Callable<span class="token punctuation">[</span><span class="token punctuation">[</span>T<span class="token punctuation">]</span><span class="token punctuation">,</span> Any<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span> Callable<span class="token punctuation">[</span><span class="token punctuation">[</span>T<span class="token punctuation">]</span><span class="token punctuation">,</span> Any<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
接收数据并执行函数
Args:
filter_func: 过滤函数为None则不过滤
Returns:
装饰器,装饰一个函数在接收到数据后执行
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> IS_MAIN_PROCESS<span class="token punctuation">:</span>
<span class="token keyword">raise</span> RuntimeError<span class="token punctuation">(</span><span class="token string">&#39;on_receive can only be used in main process&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">decorator</span><span class="token punctuation">(</span>func<span class="token punctuation">:</span> Callable<span class="token punctuation">[</span><span class="token punctuation">[</span>T<span class="token punctuation">]</span><span class="token punctuation">,</span> Any<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Callable<span class="token punctuation">[</span><span class="token punctuation">[</span>T<span class="token punctuation">]</span><span class="token punctuation">,</span> Any<span class="token punctuation">]</span><span class="token punctuation">:</span>
<span class="token keyword">global</span> _func_id
<span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">wrapper</span><span class="token punctuation">(</span>data<span class="token punctuation">:</span> T<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Any<span class="token punctuation">:</span>
<span class="token keyword">if</span> filter_func <span class="token keyword">is</span> <span class="token keyword">not</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> is_coroutine_callable<span class="token punctuation">(</span>filter_func<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> <span class="token keyword">await</span> filter_func<span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span>
<span class="token keyword">elif</span> <span class="token keyword">not</span> filter_func<span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span>
<span class="token keyword">if</span> is_coroutine_callable<span class="token punctuation">(</span>func<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token keyword">await</span> func<span class="token punctuation">(</span>data<span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> func<span class="token punctuation">(</span>data<span class="token punctuation">)</span>
_callback_funcs<span class="token punctuation">[</span>_func_id<span class="token punctuation">]</span> <span class="token operator">=</span> wrapper
<span class="token keyword">if</span> IS_MAIN_PROCESS<span class="token punctuation">:</span>
self<span class="token punctuation">.</span>_on_main_receive_func_ids<span class="token punctuation">.</span>append<span class="token punctuation">(</span>_func_id<span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
self<span class="token punctuation">.</span>_on_sub_receive_func_ids<span class="token punctuation">.</span>append<span class="token punctuation">(</span>_func_id<span class="token punctuation">)</span>
_func_id <span class="token operator">+=</span> <span class="token number">1</span>
<span class="token keyword">return</span> func
<span class="token keyword">return</span> decorator
</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 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></details><h4 id="async-def-run-on-receive-funcs-self-data-any" tabindex="-1"><a class="header-anchor" href="#async-def-run-on-receive-funcs-self-data-any"><span><em><strong>async def</strong></em> <code>_run_on_receive_funcs(self, data: Any)</code></span></a></h4><p><strong>Description</strong>: 运行接收函数</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>data: 数据</li></ul></blockquote><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L192" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">_run_on_receive_funcs</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> data<span class="token punctuation">:</span> Any<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
运行接收函数
Args:
data: 数据
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> IS_MAIN_PROCESS<span class="token punctuation">:</span>
<span class="token punctuation">[</span>asyncio<span class="token punctuation">.</span>create_task<span class="token punctuation">(</span>_callback_funcs<span class="token punctuation">[</span>func_id<span class="token punctuation">]</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">for</span> func_id <span class="token keyword">in</span> self<span class="token punctuation">.</span>_on_main_receive_func_ids<span class="token punctuation">]</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token punctuation">[</span>asyncio<span class="token punctuation">.</span>create_task<span class="token punctuation">(</span>_callback_funcs<span class="token punctuation">[</span>func_id<span class="token punctuation">]</span><span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">for</span> func_id <span class="token keyword">in</span> self<span class="token punctuation">.</span>_on_sub_receive_func_ids<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></div></details><h4 id="async-def-start-receive-loop-self" tabindex="-1"><a class="header-anchor" href="#async-def-start-receive-loop-self"><span><em><strong>async def</strong></em> <code>start_receive_loop(self)</code></span></a></h4><p><strong>Description</strong>: 开始接收数据 会自动判断主进程和子进程,需要在对应进程都调度一次</p><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L203" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">start_receive_loop</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
开始接收数据
会自动判断主进程和子进程,需要在对应进程都调度一次
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> <span class="token builtin">len</span><span class="token punctuation">(</span>self<span class="token punctuation">.</span>_on_main_receive_func_ids<span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
logger<span class="token punctuation">.</span>warning<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&#39;No on_receive function registered for </span><span class="token interpolation"><span class="token punctuation">{</span>self<span class="token punctuation">.</span>name<span class="token punctuation">}</span></span><span class="token string">&#39;</span></span><span class="token punctuation">)</span>
<span class="token keyword">return</span>
self<span class="token punctuation">.</span>is_receive_loop_running <span class="token operator">=</span> <span class="token boolean">True</span>
logger<span class="token punctuation">.</span>debug<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&#39;Starting receive loop for </span><span class="token interpolation"><span class="token punctuation">{</span>self<span class="token punctuation">.</span>name<span class="token punctuation">}</span></span><span class="token string">&#39;</span></span><span class="token punctuation">)</span>
<span class="token keyword">while</span> <span class="token keyword">not</span> self<span class="token punctuation">.</span>_closed<span class="token punctuation">:</span>
data <span class="token operator">=</span> <span class="token keyword">await</span> self<span class="token punctuation">.</span>async_receive<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">await</span> self<span class="token punctuation">.</span>_run_on_receive_funcs<span class="token punctuation">(</span>data<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></div></details><h3 id="var-active-channel" tabindex="-1"><a class="header-anchor" href="#var-active-channel"><span>var <code>active_channel</code></span></a></h3><ul><li><p><strong>Description</strong>: 子进程可用的主动和被动通道</p></li><li><p><strong>Type</strong>: <code>Channel</code></p></li><li><p><strong>Default</strong>: <code>Channel(name=&#39;active_channel&#39;)</code></p></li></ul><h3 id="var-publish-channel" tabindex="-1"><a class="header-anchor" href="#var-publish-channel"><span>var <code>publish_channel</code></span></a></h3><ul><li><p><strong>Description</strong>: 通道传递通道,主进程创建单例,子进程初始化时实例化</p></li><li><p><strong>Type</strong>: <code>Channel[tuple[str, dict[str, Any]]]</code></p></li><li><p><strong>Default</strong>: <code>Channel(name=&#39;publish_channel&#39;)</code></p></li></ul><h3 id="var-channel-deliver-active-channel" tabindex="-1"><a class="header-anchor" href="#var-channel-deliver-active-channel"><span>var <code>channel_deliver_active_channel</code></span></a></h3><ul><li><p><strong>Description</strong>: 通道传递通道,主进程创建单例,子进程初始化时实例化</p></li><li><p><strong>Type</strong>: <code>Channel[Channel[Any]]</code></p></li><li><p><strong>Default</strong>: <code>NO_DEFAULT</code></p></li></ul><h3 id="def-set-channel-name-str-channel-channel" tabindex="-1"><a class="header-anchor" href="#def-set-channel-name-str-channel-channel"><span><em><strong>def</strong></em> <code>set_channel(name: str, channel: Channel)</code></span></a></h3><p><strong>Description</strong>: 设置通道实例</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>name: 通道名称</li><li>channel: 通道实例</li></ul></blockquote><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L250" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">set_channel</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> channel<span class="token punctuation">:</span> <span class="token string">&#39;Channel&#39;</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
设置通道实例
Args:
name: 通道名称
channel: 通道实例
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> <span class="token keyword">not</span> <span class="token builtin">isinstance</span><span class="token punctuation">(</span>channel<span class="token punctuation">,</span> Channel<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">raise</span> TypeError<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&#39;channel_ must be an instance of Channel, </span><span class="token interpolation"><span class="token punctuation">{</span><span class="token builtin">type</span><span class="token punctuation">(</span>channel<span class="token punctuation">)</span><span class="token punctuation">}</span></span><span class="token string"> found&#39;</span></span><span class="token punctuation">)</span>
<span class="token keyword">if</span> IS_MAIN_PROCESS<span class="token punctuation">:</span>
<span class="token keyword">if</span> name <span class="token keyword">in</span> _channel<span class="token punctuation">:</span>
<span class="token keyword">raise</span> ValueError<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&#39;Channel </span><span class="token interpolation"><span class="token punctuation">{</span>name<span class="token punctuation">}</span></span><span class="token string"> already exists&#39;</span></span><span class="token punctuation">)</span>
_channel<span class="token punctuation">[</span>name<span class="token punctuation">]</span> <span class="token operator">=</span> channel
<span class="token keyword">else</span><span class="token punctuation">:</span>
channel_deliver_passive_channel<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">&#39;set_channel&#39;</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string">&#39;name&#39;</span><span class="token punctuation">:</span> name<span class="token punctuation">,</span> <span class="token string">&#39;channel_&#39;</span><span class="token punctuation">:</span> channel<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 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></details><h3 id="def-set-channels-channels-dict-str-channel" tabindex="-1"><a class="header-anchor" href="#def-set-channels-channels-dict-str-channel"><span><em><strong>def</strong></em> <code>set_channels(channels: dict[str, Channel])</code></span></a></h3><p><strong>Description</strong>: 设置通道实例</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>channels: 通道名称</li></ul></blockquote><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L276" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">set_channels</span><span class="token punctuation">(</span>channels<span class="token punctuation">:</span> <span class="token builtin">dict</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">,</span> <span class="token string">&#39;Channel&#39;</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
设置通道实例
Args:
channels: 通道名称
&quot;&quot;&quot;</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span>name<span class="token punctuation">,</span> channel<span class="token punctuation">)</span> <span class="token keyword">in</span> channels<span class="token punctuation">.</span>items<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
set_channel<span class="token punctuation">(</span>name<span class="token punctuation">,</span> channel<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></div></details><h3 id="def-get-channel-name-str-channel" tabindex="-1"><a class="header-anchor" href="#def-get-channel-name-str-channel"><span><em><strong>def</strong></em> <code>get_channel(name: str) -&gt; Channel</code></span></a></h3><p><strong>Description</strong>: 获取通道实例</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>name: 通道名称</li></ul></blockquote><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L286" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">get_channel</span><span class="token punctuation">(</span>name<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token string">&#39;Channel&#39;</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
获取通道实例
Args:
name: 通道名称
Returns:
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> IS_MAIN_PROCESS<span class="token punctuation">:</span>
<span class="token keyword">return</span> _channel<span class="token punctuation">[</span>name<span class="token punctuation">]</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
recv_chan <span class="token operator">=</span> Channel<span class="token punctuation">[</span>Channel<span class="token punctuation">[</span>Any<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token string">&#39;recv_chan&#39;</span><span class="token punctuation">)</span>
channel_deliver_passive_channel<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">&#39;get_channel&#39;</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string">&#39;name&#39;</span><span class="token punctuation">:</span> name<span class="token punctuation">,</span> <span class="token string">&#39;recv_chan&#39;</span><span class="token punctuation">:</span> recv_chan<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> recv_chan<span class="token punctuation">.</span>receive<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 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></details><h3 id="def-get-channels-dict-str-channel" tabindex="-1"><a class="header-anchor" href="#def-get-channels-dict-str-channel"><span><em><strong>def</strong></em> <code>get_channels() -&gt; dict[str, Channel]</code></span></a></h3><p><strong>Description</strong>: 获取通道实例</p><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L310" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">get_channels</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token builtin">dict</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">,</span> <span class="token string">&#39;Channel&#39;</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
获取通道实例
Returns:
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> IS_MAIN_PROCESS<span class="token punctuation">:</span>
<span class="token keyword">return</span> _channel
<span class="token keyword">else</span><span class="token punctuation">:</span>
recv_chan <span class="token operator">=</span> Channel<span class="token punctuation">[</span><span class="token builtin">dict</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">,</span> Channel<span class="token punctuation">[</span>Any<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">(</span><span class="token string">&#39;recv_chan&#39;</span><span class="token punctuation">)</span>
channel_deliver_passive_channel<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">&#39;get_channels&#39;</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string">&#39;recv_chan&#39;</span><span class="token punctuation">:</span> recv_chan<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> recv_chan<span class="token punctuation">.</span>receive<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 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></details><p><code>@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == &#39;set_channel&#39;)</code></p><h3 id="def-on-set-channel-data-tuple-str-dict-str-any" tabindex="-1"><a class="header-anchor" href="#def-on-set-channel-data-tuple-str-dict-str-any"><span><em><strong>def</strong></em> <code>on_set_channel(data: tuple[str, dict[str, Any]])</code></span></a></h3><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L233" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token decorator annotation punctuation">@channel_deliver_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> data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&#39;set_channel&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">on_set_channel</span><span class="token punctuation">(</span>data<span class="token punctuation">:</span> <span class="token builtin">tuple</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">,</span> <span class="token builtin">dict</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">,</span> Any<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>name<span class="token punctuation">,</span> channel<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">(</span>data<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">&#39;name&#39;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> data<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">&#39;channel_&#39;</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
set_channel<span class="token punctuation">(</span>name<span class="token punctuation">,</span> channel<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></div></details><p><code>@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == &#39;get_channel&#39;)</code></p><h3 id="def-on-get-channel-data-tuple-str-dict-str-any" tabindex="-1"><a class="header-anchor" href="#def-on-get-channel-data-tuple-str-dict-str-any"><span><em><strong>def</strong></em> <code>on_get_channel(data: tuple[str, dict[str, Any]])</code></span></a></h3><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L239" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token decorator annotation punctuation">@channel_deliver_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> data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&#39;get_channel&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">on_get_channel</span><span class="token punctuation">(</span>data<span class="token punctuation">:</span> <span class="token builtin">tuple</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">,</span> <span class="token builtin">dict</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">,</span> Any<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>name<span class="token punctuation">,</span> recv_chan<span class="token punctuation">)</span> <span class="token operator">=</span> <span class="token punctuation">(</span>data<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">&#39;name&#39;</span><span class="token punctuation">]</span><span class="token punctuation">,</span> data<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">&#39;recv_chan&#39;</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
recv_chan<span class="token punctuation">.</span>send<span class="token punctuation">(</span>get_channel<span class="token punctuation">(</span>name<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></div></details><p><code>@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == &#39;get_channels&#39;)</code></p><h3 id="def-on-get-channels-data-tuple-str-dict-str-any" tabindex="-1"><a class="header-anchor" href="#def-on-get-channels-data-tuple-str-dict-str-any"><span><em><strong>def</strong></em> <code>on_get_channels(data: tuple[str, dict[str, Any]])</code></span></a></h3><details><summary><b>Source code</b> or <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L245" target="_blank">View on GitHub</a></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token decorator annotation punctuation">@channel_deliver_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> data<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">&#39;get_channels&#39;</span><span class="token punctuation">)</span>
<span class="token keyword">def</span> <span class="token function">on_get_channels</span><span class="token punctuation">(</span>data<span class="token punctuation">:</span> <span class="token builtin">tuple</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">,</span> <span class="token builtin">dict</span><span class="token punctuation">[</span><span class="token builtin">str</span><span class="token punctuation">,</span> Any<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
recv_chan <span class="token operator">=</span> data<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">[</span><span class="token string">&#39;recv_chan&#39;</span><span class="token punctuation">]</span>
recv_chan<span class="token punctuation">.</span>send<span class="token punctuation">(</span>get_channels<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></div></details>`,78),o=[p];function c(l,i){return s(),a("div",null,o)}const r=n(e,[["render",c],["__file","channel.html.vue"]]),d=JSON.parse('{"path":"/en/dev/api/comm/channel.html","title":"liteyuki.comm.channel","lang":"en-US","frontmatter":{"title":"liteyuki.comm.channel","lastUpdated":false,"description":"liteyuki.comm.channel Description: Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved @Time : 2024/7/26 下午11:21 @Author : snowykami @Email : snowykami@outlook.com @File...","head":[["link",{"rel":"alternate","hreflang":"zh-cn","href":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/api/comm/channel.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/api/comm/channel.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot"}],["meta",{"property":"og:title","content":"liteyuki.comm.channel"}],["meta",{"property":"og:description","content":"liteyuki.comm.channel Description: Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved @Time : 2024/7/26 下午11:21 @Author : snowykami @Email : snowykami@outlook.com @File..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"en-US"}],["meta",{"property":"og:locale:alternate","content":"zh-CN"}],["meta",{"property":"og:updated_time","content":"2024-08-29T06:19:39.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-29T06:19:39.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"liteyuki.comm.channel\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-29T06:19:39.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":3,"title":"class Channel(Generic[T])","slug":"class-channel-generic-t","link":"#class-channel-generic-t","children":[]},{"level":3,"title":"var active_channel","slug":"var-active-channel","link":"#var-active-channel","children":[]},{"level":3,"title":"var publish_channel","slug":"var-publish-channel","link":"#var-publish-channel","children":[]},{"level":3,"title":"var channel_deliver_active_channel","slug":"var-channel-deliver-active-channel","link":"#var-channel-deliver-active-channel","children":[]},{"level":3,"title":"def set_channel(name: str, channel: Channel)","slug":"def-set-channel-name-str-channel-channel","link":"#def-set-channel-name-str-channel-channel","children":[]},{"level":3,"title":"def set_channels(channels: dict[str, Channel])","slug":"def-set-channels-channels-dict-str-channel","link":"#def-set-channels-channels-dict-str-channel","children":[]},{"level":3,"title":"def get_channel(name: str) -> Channel","slug":"def-get-channel-name-str-channel","link":"#def-get-channel-name-str-channel","children":[]},{"level":3,"title":"def get_channels() -> dict[str, Channel]","slug":"def-get-channels-dict-str-channel","link":"#def-get-channels-dict-str-channel","children":[]},{"level":3,"title":"def on_set_channel(data: tuple[str, dict[str, Any]])","slug":"def-on-set-channel-data-tuple-str-dict-str-any","link":"#def-on-set-channel-data-tuple-str-dict-str-any","children":[]},{"level":3,"title":"def on_get_channel(data: tuple[str, dict[str, Any]])","slug":"def-on-get-channel-data-tuple-str-dict-str-any","link":"#def-on-get-channel-data-tuple-str-dict-str-any","children":[]},{"level":3,"title":"def on_get_channels(data: tuple[str, dict[str, Any]])","slug":"def-on-get-channels-data-tuple-str-dict-str-any","link":"#def-on-get-channels-data-tuple-str-dict-str-any","children":[]}],"git":{"createdTime":1724234361000,"updatedTime":1724912379000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":2}]},"readingTime":{"minutes":5.85,"words":1754},"filePathRelative":"en/dev/api/comm/channel.md","localizedDate":"August 21, 2024","autoDesc":true}');export{r as comp,d as data};