2024-08-31 16:18:25 +08:00
|
|
|
|
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>说明</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>说明</strong>: 初始化通道</p><p><strong>参数</strong>:</p><blockquote><ul><li>name: 通道ID</li><li>type_check: 是否开启类型检查, 若为空,则传入泛型默认开启,否则默认关闭</li></ul></blockquote><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L41" target="_blank">在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>
|
2024-08-29 13:53:08 +08:00
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
初始化通道
|
|
|
|
|
Args:
|
|
|
|
|
name: 通道ID
|
|
|
|
|
type_check: 是否开启类型检查, 若为空,则传入泛型默认开启,否则默认关闭
|
|
|
|
|
"""</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">'Type hint is required for enforcing type check.'</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'Channel </span><span class="token interpolation"><span class="token punctuation">{</span>name<span class="token punctuation">}</span></span><span class="token string"> already exists'</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'Channel </span><span class="token interpolation"><span class="token punctuation">{</span>name<span class="token punctuation">}</span></span><span class="token string"> already exists'</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'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'</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'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'</span></span><span class="token punctuation">)</span>
|
2024-08-31 16:18:25 +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 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) -> Optional[type]</code></span></a></h4><p><strong>说明</strong>: 获取通道传递泛型类型</p><p><strong>返回</strong>: Optional[type]: 泛型类型</p><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L77" target="_blank">在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">></span> Optional<span class="token punctuation">[</span><span class="token builtin">type</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
|
2024-08-29 13:53:08 +08:00
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
获取通道传递泛型类型
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Optional[type]: 泛型类型
|
|
|
|
|
"""</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">'__orig_class__'</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>
|
2024-08-31 16:18:25 +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></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) -> bool</code></span></a></h4><p><strong>说明</strong>: 验证数据结构</p><p><strong>参数</strong>:</p><blockquote><ul><li>data: 数据</li><li>structure: 结构</li></ul></blockquote><p><strong>返回</strong>: bool: 是否通过验证</p><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L88" target="_blank">在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">></span> <span class="token builtin">bool</span><span class="token punctuation">:</span>
|
2024-08-29 13:53:08 +08:00
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
验证数据结构
|
|
|
|
|
Args:
|
|
|
|
|
data: 数据
|
|
|
|
|
structure: 结构
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
bool: 是否通过验证
|
|
|
|
|
"""</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>
|
2024-08-31 16:18:25 +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 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>说明</strong>: 发送数据,发送函数为同步函数,没有异步的必要</p><p><strong>参数</strong>:</p><blockquote><ul><li>data: 数据</li></ul></blockquote><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L117" target="_blank">在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>
|
2024-08-29 13:53:08 +08:00
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
发送数据,发送函数为同步函数,没有异步的必要
|
|
|
|
|
Args:
|
|
|
|
|
data: 数据
|
|
|
|
|
"""</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'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'</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">'Cannot send to a closed channel_'</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>
|
2024-08-31 16:18:25 +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></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) -> T</code></span></a></h4><p><strong>说明</strong>: 同步接收数据,会阻塞线程</p><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L132" target="_blank">在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">></span> T<span class="token punctuation">:</span>
|
2024-08-29 13:53:08 +08:00
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
同步接收数据,会阻塞线程
|
|
|
|
|
Args:
|
|
|
|
|
"""</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">'Cannot receive from a closed channel_'</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
|
2024-08-31 16:18:25 +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></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) -> T</code></span></a></h4><p><strong>说明</strong>: 异步接收数据,会挂起等待</p><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L144" target="_blank">在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">></span> T<span class="token punctuation">:</span>
|
2024-08-29 13:53:08 +08:00
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
异步接收数据,会挂起等待
|
|
|
|
|
"""</span>
|
|
|
|
|
<span class="token keyword">print</span><span class="token punctuation">(</span><span class="token string">'等待接收数据'</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">'接收到数据'</span><span class="token punctuation">)</span>
|
|
|
|
|
<span class="token keyword">return</span> data
|
2024-08-31 16:18:25 +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></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) -> Callable[[Callable[[T], Any]], Callable[[T], Any]]</code></span></a></h4><p><strong>说明</strong>: 接收数据并执行函数</p><p><strong>参数</strong>:</p><blockquote><ul><li>filter_func: 过滤函数,为None则不过滤</li></ul></blockquote><p><strong>返回</strong>: 装饰器,装饰一个函数在接收到数据后执行</p><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L154" target="_blank">在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">></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>
|
2024-08-29 13:53:08 +08:00
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
接收数据并执行函数
|
|
|
|
|
Args:
|
|
|
|
|
filter_func: 过滤函数,为None则不过滤
|
|
|
|
|
Returns:
|
|
|
|
|
装饰器,装饰一个函数在接收到数据后执行
|
|
|
|
|
"""</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">'on_receive can only be used in main process'</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">></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">></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
|
2024-08-31 16:18:25 +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 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>说明</strong>: 运行接收函数</p><p><strong>参数</strong>:</p><blockquote><ul><li>data: 数据</li></ul></blockquote><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L192" target="_blank">在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>
|
2024-08-29 13:53:08 +08:00
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
运行接收函数
|
|
|
|
|
Args:
|
|
|
|
|
data: 数据
|
|
|
|
|
"""</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>
|
2024-08-31 16:18:25 +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></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>说明</strong>: 开始接收数据 会自动判断主进程和子进程,需要在对应进程都调度一次</p><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L203" target="_blank">在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>
|
2024-08-29 13:53:08 +08:00
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
开始接收数据
|
|
|
|
|
会自动判断主进程和子进程,需要在对应进程都调度一次
|
|
|
|
|
"""</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'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">'</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'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">'</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>
|
2024-08-31 16:18:25 +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></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>说明</strong>: 子进程可用的主动和被动通道</p></li><li><p><strong>类型</strong>: <code>Channel</code></p></li><li><p><strong>默认值</strong>: <code>Channel(name='active_channel')</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>说明</strong>: 通道传递通道,主进程创建单例,子进程初始化时实例化</p></li><li><p><strong>类型</strong>: <code>Channel[tuple[str, dict[str, Any]]]</code></p></li><li><p><strong>默认值</strong>: <code>Channel(name='publish_channel')</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>说明</strong>: 通道传递通道,主进程创建单例,子进程初始化时实例化</p></li><li><p><strong>类型</strong>: <code>Channel[Channel[Any]]</code></p></li><li><p><strong>默认值</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>说明</strong>: 设置通道实例</p><p><strong>参数</strong>:</p><blockquote><ul><li>name: 通道名称</li><li>channel: 通道实例</li></ul></blockquote><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L250" target="_blank">在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">'Channel'</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
|
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
设置通道实例
|
|
|
|
|
Args:
|
|
|
|
|
name: 通道名称
|
|
|
|
|
channel: 通道实例
|
|
|
|
|
"""</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'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'</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'Channel </span><span class="token interpolation"><span class="token punctuation">{</span>name<span class="token punctuation">}</span></span><span class="token string"> already exists'</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">'set_channel'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string">'name'</span><span class="token punctuation">:</span> name<span class="token punctuation">,</span> <span class="token string">'channel_'</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>说明</strong>: 设置通道实例</p><p><strong>参数</strong>:</p><blockquote><ul><li>channels: 通道名称</li></ul></blockquote><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L276" target="_blank">在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">'Channel'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
|
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
设置通道实例
|
|
|
|
|
Args:
|
|
|
|
|
channels: 通道名称
|
|
|
|
|
"""</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) -> Channel</code></span></a></h3><p><strong>说明</strong>: 获取通道实例</p><p><strong>参数</strong>:</p><blockquote><ul><li>name: 通道名称</li></ul></blockquote><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L286" target="_blank">在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">></span> <span class="token string">'Channel'</span><span class="token punctuation">:</span>
|
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
获取通道实例
|
|
|
|
|
Args:
|
|
|
|
|
name: 通道名称
|
|
|
|
|
Returns:
|
|
|
|
|
"""</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">'recv_chan'</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">'get_channel'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string">'name'</span><span class="token punctuation">:</span> name<span class="token punctuation">,</span> <span class="token string">'recv_chan'</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() -> dict[str, Channel]</code></span></a></h3><p><strong>说明</strong>: 获取通道实例</p><details><summary><b>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L310" target="_blank">在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">></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">'Channel'</span><span class="token punctuation">]</span><span class="token punctuation">:</span>
|
|
|
|
|
<span class="token triple-quoted-string string">"""
|
|
|
|
|
获取通道实例
|
|
|
|
|
Returns:
|
|
|
|
|
"""</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">'recv_chan'</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">'get_channels'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string">'recv_chan'</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] == 'set_channel')</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>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L233" target="_blank">在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">'set_channel'</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">'name'</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">'channel_'</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] == 'get_channel')</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>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L239" target="_blank">在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">'get_channel'</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">'name'</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">'recv_chan'</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] == 'get_channels')</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>源代码</b> 或 <a href="https://github.com/LiteyukiStudio/LiteyukiBot/tree/main/liteyuki/comm/channel.py#L245" target="_blank">在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">'get_channels'</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">'recv_chan'</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":"/dev/api/comm/channel.html","title":"liteyuki.comm.channel","lang":"zh-CN","frontmatter":{"title":"liteyuki.comm.channel","lastUpdated":false,"description":"liteyuki.comm.channel 说明: Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved @Time : 2024/7/26 下午11:21 @Author : snowykami @Email : snowykami@outlook.com @File : channe...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/api/comm/channel.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/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 说明: Copyright (C) 2020-2024 LiteyukiStudio. All Rights Reserved @Time : 2024/7/26 下午11:21 @Author : snowykami @Email : snowykami@outlook.com @File : channe..."}],["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: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":1724031826000,"updatedTime":1724912379000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":7}]},"readingTime":{"minutes":6.13,"words":1839},"filePathRelative":"dev/api/comm/channel.md","localizedDate":"2024年8月19日","autoDesc":true}');export{r as comp,d as data};
|