forked from bot/app
213 lines
68 KiB
JavaScript
213 lines
68 KiB
JavaScript
import{_ as n,o as s,c as a,e}from"./app-BiKNfxRB.js";const t={},p=e(`<h3 id="func-set-channel" tabindex="-1"><a class="header-anchor" href="#func-set-channel"><span><em>func</em> <code>set_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></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="func-set-channels" tabindex="-1"><a class="header-anchor" href="#func-set-channels"><span><em>func</em> <code>set_channels()</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></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="func-get-channel-channel" tabindex="-1"><a class="header-anchor" href="#func-get-channel-channel"><span><em>func</em> <code>get_channel() -> 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></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="func-get-channels-dict-str-channel" tabindex="-1"><a class="header-anchor" href="#func-get-channels-dict-str-channel"><span><em>func</em> <code>get_channels() -> dict[str, Channel]</code></span></a></h3><p><strong>Description</strong>: 获取通道实例</p><details><summary><b>Source code</b></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><h3 id="channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-set-channel" tabindex="-1"><a class="header-anchor" href="#channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-set-channel"><span><code>@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == 'set_channel')</code></span></a></h3><h3 id="func-on-set-channel" tabindex="-1"><a class="header-anchor" href="#func-on-set-channel"><span><em>func</em> <code>on_set_channel()</code></span></a></h3><details><summary><b>Source code</b></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><h3 id="channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-get-channel" tabindex="-1"><a class="header-anchor" href="#channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-get-channel"><span><code>@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == 'get_channel')</code></span></a></h3><h3 id="func-on-get-channel" tabindex="-1"><a class="header-anchor" href="#func-on-get-channel"><span><em>func</em> <code>on_get_channel()</code></span></a></h3><details><summary><b>Source code</b></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><h3 id="channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-get-channels" tabindex="-1"><a class="header-anchor" href="#channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-get-channels"><span><code>@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == 'get_channels')</code></span></a></h3><h3 id="func-on-get-channels" tabindex="-1"><a class="header-anchor" href="#func-on-get-channels"><span><em>func</em> <code>on_get_channels()</code></span></a></h3><details><summary><b>Source code</b></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><h3 id="class-channel-generic-t" tabindex="-1"><a class="header-anchor" href="#class-channel-generic-t"><span><strong>class</strong> <code>Channel(Generic[T])</code></span></a></h3><h3 id="method-init-self-name-str-type-check-optional-bool-none" tabindex="-1"><a class="header-anchor" href="#method-init-self-name-str-type-check-optional-bool-none"><span><em>method</em> <code>__init__(self, name: str, type_check: Optional[bool] = None)</code></span></a></h3><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></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">"""
|
||
初始化通道
|
||
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>
|
||
</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><h3 id="method-get-generic-type-self-optional-type" tabindex="-1"><a class="header-anchor" href="#method-get-generic-type-self-optional-type"><span><em>method</em> <code>_get_generic_type(self) -> Optional[type]</code></span></a></h3><p><strong>Description</strong>: 获取通道传递泛型类型</p><p><strong>Return</strong>: Optional[type]: 泛型类型</p><details><summary><b>Source code</b></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>
|
||
<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>
|
||
</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><h3 id="method-validate-structure-self-data-any-structure-type-bool" tabindex="-1"><a class="header-anchor" href="#method-validate-structure-self-data-any-structure-type-bool"><span><em>method</em> <code>_validate_structure(self, data: Any, structure: type) -> bool</code></span></a></h3><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></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>
|
||
<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>
|
||
</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><h3 id="method-send-self-data-t" tabindex="-1"><a class="header-anchor" href="#method-send-self-data-t"><span><em>method</em> <code>send(self, data: T)</code></span></a></h3><p><strong>Description</strong>: 发送数据,发送函数为同步函数,没有异步的必要</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>data: 数据</li></ul></blockquote><details><summary><b>Source code</b></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">"""
|
||
发送数据,发送函数为同步函数,没有异步的必要
|
||
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>
|
||
</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="method-receive-self-t" tabindex="-1"><a class="header-anchor" href="#method-receive-self-t"><span><em>method</em> <code>receive(self) -> T</code></span></a></h3><p><strong>Description</strong>: 同步接收数据,会阻塞线程</p><details><summary><b>Source code</b></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>
|
||
<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
|
||
</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><h3 id="async-method-async-receive-self-t" tabindex="-1"><a class="header-anchor" href="#async-method-async-receive-self-t"><span><em>async method</em> <code>async_receive(self) -> T</code></span></a></h3><p><strong>Description</strong>: 异步接收数据,会挂起等待</p><details><summary><b>Source code</b></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>
|
||
<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
|
||
</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><h3 id="method-on-receive-self-filter-func-optional-filter-func-none-callable-callable-t-any-callable-t-any" tabindex="-1"><a class="header-anchor" href="#method-on-receive-self-filter-func-optional-filter-func-none-callable-callable-t-any-callable-t-any"><span><em>method</em> <code>on_receive(self, filter_func: Optional[FILTER_FUNC] = None) -> Callable[[Callable[[T], Any]], Callable[[T], Any]]</code></span></a></h3><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></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>
|
||
<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
|
||
</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><h3 id="async-method-run-on-receive-funcs-self-data-any" tabindex="-1"><a class="header-anchor" href="#async-method-run-on-receive-funcs-self-data-any"><span><em>async method</em> <code>_run_on_receive_funcs(self, data: Any)</code></span></a></h3><p><strong>Description</strong>: 运行接收函数</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>data: 数据</li></ul></blockquote><details><summary><b>Source code</b></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">"""
|
||
运行接收函数
|
||
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>
|
||
</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><h3 id="async-method-start-receive-loop-self" tabindex="-1"><a class="header-anchor" href="#async-method-start-receive-loop-self"><span><em>async method</em> <code>start_receive_loop(self)</code></span></a></h3><p><strong>Description</strong>: 开始接收数据 会自动判断主进程和子进程,需要在对应进程都调度一次</p><details><summary><b>Source code</b></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">"""
|
||
开始接收数据
|
||
会自动判断主进程和子进程,需要在对应进程都调度一次
|
||
"""</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>
|
||
</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-channel-name-active-channel" tabindex="-1"><a class="header-anchor" href="#var-active-channel-channel-name-active-channel"><span><em><strong>var</strong></em> <code>active_channel = Channel(name='active_channel')</code></span></a></h3><ul><li><p><strong>Type</strong>: <code>Channel</code></p></li><li><p><strong>Description</strong>: 子进程可用的主动和被动通道</p></li></ul><h3 id="var-publish-channel-channel-name-publish-channel" tabindex="-1"><a class="header-anchor" href="#var-publish-channel-channel-name-publish-channel"><span><em><strong>var</strong></em> <code>publish_channel = Channel(name='publish_channel')</code></span></a></h3><ul><li><p><strong>Type</strong>: <code>Channel[tuple[str, dict[str, Any]]]</code></p></li><li><p><strong>Description</strong>: 通道传递通道,主进程创建单例,子进程初始化时实例化</p></li></ul><h3 id="var-channel-deliver-active-channel-no-default" tabindex="-1"><a class="header-anchor" href="#var-channel-deliver-active-channel-no-default"><span><em><strong>var</strong></em> <code>channel_deliver_active_channel = NO_DEFAULT</code></span></a></h3><ul><li><p><strong>Type</strong>: <code>Channel[Channel[Any]]</code></p></li><li><p><strong>Description</strong>: 通道传递通道,主进程创建单例,子进程初始化时实例化</p></li></ul>`,74),c=[p];function o(l,i){return s(),a("div",null,c)}const r=n(t,[["render",o],["__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","description":"func set_channel() Description: 设置通道实例 Arguments: name: 通道名称 channel: 通道实例 Source code func set_channels() Description: 设置通道实例 Arguments: channels: 通道名称 Source code func get_cha...","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":"func set_channel() Description: 设置通道实例 Arguments: name: 通道名称 channel: 通道实例 Source code func set_channels() Description: 设置通道实例 Arguments: channels: 通道名称 Source code func get_cha..."}],["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":"func set_channel()","slug":"func-set-channel","link":"#func-set-channel","children":[]},{"level":3,"title":"func set_channels()","slug":"func-set-channels","link":"#func-set-channels","children":[]},{"level":3,"title":"func get_channel() -> Channel","slug":"func-get-channel-channel","link":"#func-get-channel-channel","children":[]},{"level":3,"title":"func get_channels() -> dict[str, Channel]","slug":"func-get-channels-dict-str-channel","link":"#func-get-channels-dict-str-channel","children":[]},{"level":3,"title":"@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == 'set_channel')","slug":"channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-set-channel","link":"#channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-set-channel","children":[]},{"level":3,"title":"func on_set_channel()","slug":"func-on-set-channel","link":"#func-on-set-channel","children":[]},{"level":3,"title":"@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == 'get_channel')","slug":"channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-get-channel","link":"#channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-get-channel","children":[]},{"level":3,"title":"func on_get_channel()","slug":"func-on-get-channel","link":"#func-on-get-channel","children":[]},{"level":3,"title":"@channel_deliver_passive_channel.on_receive(filter_func=lambda data: data[0] == 'get_channels')","slug":"channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-get-channels","link":"#channel-deliver-passive-channel-on-receive-filter-func-lambda-data-data-0-get-channels","children":[]},{"level":3,"title":"func on_get_channels()","slug":"func-on-get-channels","link":"#func-on-get-channels","children":[]},{"level":3,"title":"class Channel(Generic[T])","slug":"class-channel-generic-t","link":"#class-channel-generic-t","children":[]},{"level":3,"title":"method __init__(self, name: str, type_check: Optional[bool] = None)","slug":"method-init-self-name-str-type-check-optional-bool-none","link":"#method-init-self-name-str-type-check-optional-bool-none","children":[]},{"level":3,"title":"method _get_generic_type(self) -> Optional[type]","slug":"method-get-generic-type-self-optional-type","link":"#method-get-generic-type-self-optional-type","children":[]},{"level":3,"title":"method _validate_structure(self, data: Any, structure: type) -> bool","slug":"method-validate-structure-self-data-any-structure-type-bool","link":"#method-validate-structure-self-data-any-structure-type-bool","children":[]},{"level":3,"title":"method send(self, data: T)","slug":"method-send-self-data-t","link":"#method-send-self-data-t","children":[]},{"level":3,"title":"method receive(self) -> T","slug":"method-receive-self-t","link":"#method-receive-self-t","children":[]},{"level":3,"title":"async method async_receive(self) -> T","slug":"async-method-async-receive-self-t","link":"#async-method-async-receive-self-t","children":[]},{"level":3,"title":"method on_receive(self, filter_func: Optional[FILTER_FUNC] = None) -> Callable[[Callable[[T], Any]], Callable[[T], Any]]","slug":"method-on-receive-self-filter-func-optional-filter-func-none-callable-callable-t-any-callable-t-any","link":"#method-on-receive-self-filter-func-optional-filter-func-none-callable-callable-t-any-callable-t-any","children":[]},{"level":3,"title":"async method _run_on_receive_funcs(self, data: Any)","slug":"async-method-run-on-receive-funcs-self-data-any","link":"#async-method-run-on-receive-funcs-self-data-any","children":[]},{"level":3,"title":"async method start_receive_loop(self)","slug":"async-method-start-receive-loop-self","link":"#async-method-start-receive-loop-self","children":[]},{"level":3,"title":"var active_channel = Channel(name='active_channel')","slug":"var-active-channel-channel-name-active-channel","link":"#var-active-channel-channel-name-active-channel","children":[]},{"level":3,"title":"var publish_channel = Channel(name='publish_channel')","slug":"var-publish-channel-channel-name-publish-channel","link":"#var-publish-channel-channel-name-publish-channel","children":[]},{"level":3,"title":"var channel_deliver_active_channel = NO_DEFAULT","slug":"var-channel-deliver-active-channel-no-default","link":"#var-channel-deliver-active-channel-no-default","children":[]}],"git":{"createdTime":1724234361000,"updatedTime":1724912379000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":2}]},"readingTime":{"minutes":4.95,"words":1484},"filePathRelative":"en/dev/api/comm/channel.md","localizedDate":"August 21, 2024","autoDesc":true}`);export{r as comp,d as data};
|