forked from bot/app
213 lines
73 KiB
JavaScript
213 lines
73 KiB
JavaScript
|
import{_ as n,o as a,c as s,d as e}from"./app-EhCe7g9m.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>说明</strong>: 设置通道实例</p><p><strong>参数</strong>:</p><blockquote><ul><li>name: 通道名称</li><li>channel: 通道实例</li></ul></blockquote><details><summary><b>源代码</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>说明</strong>: 设置通道实例</p><p><strong>参数</strong>:</p><blockquote><ul><li>channels: 通道名称</li></ul></blockquote><details><summary><b>源代码</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>说明</strong>: 获取通道实例</p><p><strong>参数</strong>:</p><blockquote><ul><li>name: 通道名称</li></ul></blockquote><details><summary><b>源代码</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>说明</strong>: 获取通道实例</p><details><summary><b>源代码</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>源代码</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>源代码</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>源代码</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>说明</strong>: 初始化通道</p><p><strong>参数</strong>:</p><blockquote><ul><li>name: 通道ID</li><li>type_check: 是否开启类型检查, 若为空,则传入泛型默认开启,否则默认关闭</li></ul></blockquote><details><summary><b>源代码</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>说明</strong>: 获取通道传递泛型类型</p><p><strong>返回</strong>: Optional[type]: 泛型类型</p><details><summary><b>源代码</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>说明</strong>: 验证数据结构</p><p><strong>返回</strong>: bool: 是否通过验证</p><p><strong>参数</strong>:</p><blockquote><ul><li>data: 数据</li><li>structure: 结构</li></ul></blockquote><details><summary><b>源代码</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>说明</strong>: 发送数据,发送函数为同步函数,没有异步的必要</p><p><strong>参数</strong>:</p><blockquote><ul><li>data: 数据</li></ul></blockquote><details><summary><b>源代码</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>说明</strong>: 同步接收数据,会阻塞线程</p><details><summary><b>源代码</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>说明</strong>: 异步接收数据,会挂起等待</p><details><summary><b>源代码</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>说明</strong>: 接收数据并执行函数</p><p><strong>返回</strong>: 装饰器,装饰一个函数在接收到数据后执行</p><p><strong>参数</strong>:</p><blockquote><ul><li>filter_func: 过滤函数,为None则不过滤</li></ul></blockquote><details><summary><b>源代码</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>说明</strong>: 运行接收函数</p><p><strong>参数</strong>:</p><blockquote><ul><li>data: 数据</li></ul></blockquote><details><summary><b>源代码</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>说明</strong>: 开始接收数据 会自动判断主进程和子进程,需要在对应进程都调度一次</p><details><summary><b>源代码</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-sync-on-receive-func-callable-t-any" tabindex="-1"><a class="header-anchor" href="#var-sync-on-receive-func-callable-t-any"><span><em><strong>var</strong></em> <code>SYNC_ON_RECEIVE_FUNC = Callable[[T], Any]</code></span></a></h3><ul><li><strong>类型</strong>: <code>TypeAlias</code></li></ul><h3 id="var-async-on-receive-func-callable-t-coroutine-any-any-any" tabindex="-1"><a class="header-anchor" href="#var-async-on-receive-func-callable-t-coroutine-any-any-any"><span><em><strong>var</strong></em> <code>ASYNC_ON_RECEIVE_FUNC = Callable[[T], Coroutine[Any, Any, Any]]</code></span></a></h3><ul><li><strong>类型</strong>: <code>TypeAlias</code></li></ul><h3 id="var-on-receive-func-sync-on-receive-func-async-on-receive-func" tabindex="-1"><a class="header-anchor" href="#var-on-receive-func-sync-on-receive-func-async-on-receive-func"><span><em><strong>var</strong></em> <code>ON_RECEIVE_FUNC = SYNC_ON_RECEIVE_FUNC | ASYNC_ON_RECEIVE_FUNC</code></span></a></h3><ul><li><strong>类型</strong>: <code>TypeAlias</code></li></ul><h3 id="var-sync-filter-func-callable-t-bool" tabindex="-1"><a class="header-anchor" href="#var-sync-filter-func-callable-t-bool"><span><em><strong>var</strong></em> <code>SYNC_FILTER_FUNC = Callable[[T], bool]</code></span></a></h3><ul><li><strong>类型</strong>: <code>TypeAlias</code></li></ul><h3 id="var-async-filter-func-callable-t-coroutine-any-any-bool" tabindex="-1"><a class="header-anchor" href="#var-async-filter-func-callable-t-coroutine-any-any-bool"><span><em><strong>var</strong></em> <code>ASYNC_FILTER_FUNC = Callable[[T], Coroutine[Any, Any, bool]]</code></span></a></h3><ul><li><strong>类型</strong>: <code>TypeAlias</code></li></ul><h3 id="var-filter-func-sync-filter-func-async-filter-func" tabindex="-1"><a class="header-anchor" href="#var-filter-func-sync-filter-func-async-filter-func"><span><em><strong>var</strong></em> <code>FILTER_FUNC = SYNC_FILTER_FUNC | ASYNC_FILTER_FUNC</code></span></a></h3><ul><li><strong>类型</strong>: <code>TypeAlias</code></li></ul><h3 id="var-func-id-0" tabindex="-1"><a class="header-anchor" href="#var-func-id-0"><span><em><strong>var</strong></em> <code>_func_id = 0</code></span></a></h3><ul><li><strong>类型</strong>: <code>int</code></li></ul><h3 id="var-channel" tabindex="-1"><a class="header-anchor" href="#var-channel"><span><em><strong>var</strong></em> <code>_channel = {}</code></span></a></h3><ul><li><strong>类型</strong>: <code>dict[str, 'Channel']</code></li></ul><h3 id="var-callback-funcs" tabindex="-1"><a class="header-anchor" href="#var-callback-funcs"><span><em><strong>var</strong></em> <code>_callback_funcs = {}</code></span></a></h3><ul><li><strong>类型</strong>: <code>dict[int, ON_RECEIVE_FUNC]</code></li></ul><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>类型</strong>: <code>Channel</code></p></li><li><p><strong>说明</strong>: 子进程可用的主动和被动通道</p></li></ul><h3 id="var-passive-channel-channel-name-passive-channel" tabindex="-1"><a class="header-anchor" href="#var-passive-channel-channel-name-passive-channel"><span><em><strong>var</strong></em> <code>passive_channel = Channel(name='passive_channel')</code></span></a></h3><ul><li><strong>类型</strong>: <code>Channel</code></li></ul><h3 id="var-publish-channel-channel-name-publish-channel" tabindex="-1"><a class="header
|