forked from bot/app
159 lines
52 KiB
JavaScript
159 lines
52 KiB
JavaScript
import{_ as n,o as s,c as a,e}from"./app-BrSLFMno.js";const t={},p=e(`<h3 id="shared-memory-passive-chan-on-receive-lambda-d-d-0-get" tabindex="-1"><a class="header-anchor" href="#shared-memory-passive-chan-on-receive-lambda-d-d-0-get"><span><code>@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'get')</code></span></a></h3><h3 id="func-on-get" tabindex="-1"><a class="header-anchor" href="#func-on-get"><span><em>func</em> <code>on_get()</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">@shared_memory<span class="token punctuation">.</span>passive_chan<span class="token punctuation">.</span>on_receive</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> d<span class="token punctuation">:</span> d<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'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_get</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>
|
|
key <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">'key'</span><span class="token punctuation">]</span>
|
|
default <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">'default'</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>shared_memory<span class="token punctuation">.</span>get<span class="token punctuation">(</span>key<span class="token punctuation">,</span> default<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></div></details><h3 id="shared-memory-passive-chan-on-receive-lambda-d-d-0-set" tabindex="-1"><a class="header-anchor" href="#shared-memory-passive-chan-on-receive-lambda-d-d-0-set"><span><code>@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'set')</code></span></a></h3><h3 id="func-on-set" tabindex="-1"><a class="header-anchor" href="#func-on-set"><span><em>func</em> <code>on_set()</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">@shared_memory<span class="token punctuation">.</span>passive_chan<span class="token punctuation">.</span>on_receive</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> d<span class="token punctuation">:</span> d<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'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_set</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>
|
|
key <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">'key'</span><span class="token punctuation">]</span>
|
|
value <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">'value'</span><span class="token punctuation">]</span>
|
|
shared_memory<span class="token punctuation">.</span><span class="token builtin">set</span><span class="token punctuation">(</span>key<span class="token punctuation">,</span> value<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></div></details><h3 id="shared-memory-passive-chan-on-receive-lambda-d-d-0-delete" tabindex="-1"><a class="header-anchor" href="#shared-memory-passive-chan-on-receive-lambda-d-d-0-delete"><span><code>@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'delete')</code></span></a></h3><h3 id="func-on-delete" tabindex="-1"><a class="header-anchor" href="#func-on-delete"><span><em>func</em> <code>on_delete()</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">@shared_memory<span class="token punctuation">.</span>passive_chan<span class="token punctuation">.</span>on_receive</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> d<span class="token punctuation">:</span> d<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">'delete'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_delete</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>
|
|
key <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">'key'</span><span class="token punctuation">]</span>
|
|
shared_memory<span class="token punctuation">.</span>delete<span class="token punctuation">(</span>key<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="shared-memory-passive-chan-on-receive-lambda-d-d-0-get-all" tabindex="-1"><a class="header-anchor" href="#shared-memory-passive-chan-on-receive-lambda-d-d-0-get-all"><span><code>@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'get_all')</code></span></a></h3><h3 id="func-on-get-all" tabindex="-1"><a class="header-anchor" href="#func-on-get-all"><span><em>func</em> <code>on_get_all()</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">@shared_memory<span class="token punctuation">.</span>passive_chan<span class="token punctuation">.</span>on_receive</span><span class="token punctuation">(</span><span class="token keyword">lambda</span> d<span class="token punctuation">:</span> d<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_all'</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">def</span> <span class="token function">on_get_all</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>shared_memory<span class="token punctuation">.</span>get_all<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-keyvaluestore" tabindex="-1"><a class="header-anchor" href="#class-keyvaluestore"><span><strong>class</strong> <code>KeyValueStore</code></span></a></h3><h3 id="method-init-self" tabindex="-1"><a class="header-anchor" href="#method-init-self"><span><em>method</em> <code>__init__(self)</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 keyword">def</span> <span class="token function">__init__</span><span class="token punctuation">(</span>self<span class="token punctuation">)</span><span class="token punctuation">:</span>
|
|
self<span class="token punctuation">.</span>_store <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
|
|
self<span class="token punctuation">.</span>active_chan <span class="token operator">=</span> Channel<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> Optional<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 operator">=</span><span class="token string">'shared_memory-active'</span><span class="token punctuation">)</span>
|
|
self<span class="token punctuation">.</span>passive_chan <span class="token operator">=</span> Channel<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> Optional<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 operator">=</span><span class="token string">'shared_memory-passive'</span><span class="token punctuation">)</span>
|
|
self<span class="token punctuation">.</span>publish_channel <span class="token operator">=</span> Channel<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> Any<span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">(</span>name<span class="token operator">=</span><span class="token string">'shared_memory-publish'</span><span class="token punctuation">)</span>
|
|
self<span class="token punctuation">.</span>is_main_receive_loop_running <span class="token operator">=</span> <span class="token boolean">False</span>
|
|
self<span class="token punctuation">.</span>is_sub_receive_loop_running <span class="token operator">=</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></div></details><h3 id="method-set-self-key-str-value-any-none" tabindex="-1"><a class="header-anchor" href="#method-set-self-key-str-value-any-none"><span><em>method</em> <code>set(self, key: str, value: Any) -> None</code></span></a></h3><p><strong>说明</strong>: 设置键值对</p><p><strong>参数</strong>:</p><blockquote><ul><li>key: 键</li><li>value: 值</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</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> key<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> value<span class="token punctuation">:</span> Any<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token boolean">None</span><span class="token punctuation">:</span>
|
|
<span class="token triple-quoted-string string">"""
|
|
设置键值对
|
|
Args:
|
|
key: 键
|
|
value: 值
|
|
|
|
"""</span>
|
|
<span class="token keyword">if</span> IS_MAIN_PROCESS<span class="token punctuation">:</span>
|
|
lock <span class="token operator">=</span> _get_lock<span class="token punctuation">(</span>key<span class="token punctuation">)</span>
|
|
<span class="token keyword">with</span> lock<span class="token punctuation">:</span>
|
|
self<span class="token punctuation">.</span>_store<span class="token punctuation">[</span>key<span class="token punctuation">]</span> <span class="token operator">=</span> value
|
|
<span class="token keyword">else</span><span class="token punctuation">:</span>
|
|
self<span class="token punctuation">.</span>passive_chan<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">'set'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string">'key'</span><span class="token punctuation">:</span> key<span class="token punctuation">,</span> <span class="token string">'value'</span><span class="token punctuation">:</span> value<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></div></details><h3 id="method-get-self-key-str-default-optional-any-none-optional-any" tabindex="-1"><a class="header-anchor" href="#method-get-self-key-str-default-optional-any-none-optional-any"><span><em>method</em> <code>get(self, key: str, default: Optional[Any] = None) -> Optional[Any]</code></span></a></h3><p><strong>说明</strong>: 获取键值对</p><p><strong>参数</strong>:</p><blockquote><ul><li>key: 键</li><li>default: 默认值</li></ul></blockquote><p><strong>返回</strong>: Any: 值</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</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> key<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> default<span class="token punctuation">:</span> Optional<span class="token punctuation">[</span>Any<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> Optional<span class="token punctuation">[</span>Any<span class="token punctuation">]</span><span class="token punctuation">:</span>
|
|
<span class="token triple-quoted-string string">"""
|
|
获取键值对
|
|
Args:
|
|
key: 键
|
|
default: 默认值
|
|
|
|
Returns:
|
|
Any: 值
|
|
"""</span>
|
|
<span class="token keyword">if</span> IS_MAIN_PROCESS<span class="token punctuation">:</span>
|
|
lock <span class="token operator">=</span> _get_lock<span class="token punctuation">(</span>key<span class="token punctuation">)</span>
|
|
<span class="token keyword">with</span> lock<span class="token punctuation">:</span>
|
|
<span class="token keyword">return</span> self<span class="token punctuation">.</span>_store<span class="token punctuation">.</span>get<span class="token punctuation">(</span>key<span class="token punctuation">,</span> default<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>Optional<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>
|
|
self<span class="token punctuation">.</span>passive_chan<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">'get'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string">'key'</span><span class="token punctuation">:</span> key<span class="token punctuation">,</span> <span class="token string">'default'</span><span class="token punctuation">:</span> default<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 class="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-delete-self-key-str-ignore-key-error-bool-true-none" tabindex="-1"><a class="header-anchor" href="#method-delete-self-key-str-ignore-key-error-bool-true-none"><span><em>method</em> <code>delete(self, key: str, ignore_key_error: bool = True) -> None</code></span></a></h3><p><strong>说明</strong>: 删除键值对</p><p><strong>参数</strong>:</p><blockquote><ul><li>key: 键</li><li>ignore_key_error: 是否忽略键不存在的错误</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">delete</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> key<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> ignore_key_error<span class="token punctuation">:</span> <span class="token builtin">bool</span><span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token boolean">None</span><span class="token punctuation">:</span>
|
|
<span class="token triple-quoted-string string">"""
|
|
删除键值对
|
|
Args:
|
|
key: 键
|
|
ignore_key_error: 是否忽略键不存在的错误
|
|
|
|
Returns:
|
|
"""</span>
|
|
<span class="token keyword">if</span> IS_MAIN_PROCESS<span class="token punctuation">:</span>
|
|
lock <span class="token operator">=</span> _get_lock<span class="token punctuation">(</span>key<span class="token punctuation">)</span>
|
|
<span class="token keyword">with</span> lock<span class="token punctuation">:</span>
|
|
<span class="token keyword">if</span> key <span class="token keyword">in</span> self<span class="token punctuation">.</span>_store<span class="token punctuation">:</span>
|
|
<span class="token keyword">try</span><span class="token punctuation">:</span>
|
|
<span class="token keyword">del</span> self<span class="token punctuation">.</span>_store<span class="token punctuation">[</span>key<span class="token punctuation">]</span>
|
|
<span class="token keyword">del</span> _locks<span class="token punctuation">[</span>key<span class="token punctuation">]</span>
|
|
<span class="token keyword">except</span> KeyError <span class="token keyword">as</span> e<span class="token punctuation">:</span>
|
|
<span class="token keyword">if</span> <span class="token keyword">not</span> ignore_key_error<span class="token punctuation">:</span>
|
|
<span class="token keyword">raise</span> e
|
|
<span class="token keyword">else</span><span class="token punctuation">:</span>
|
|
self<span class="token punctuation">.</span>passive_chan<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">'delete'</span><span class="token punctuation">,</span> <span class="token punctuation">{</span><span class="token string">'key'</span><span class="token punctuation">:</span> key<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 class="line-number"></div><div class="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-all-self-dict-str-any" tabindex="-1"><a class="header-anchor" href="#method-get-all-self-dict-str-any"><span><em>method</em> <code>get_all(self) -> dict[str, Any]</code></span></a></h3><p><strong>说明</strong>: 获取所有键值对</p><p><strong>返回</strong>: dict[str, Any]: 键值对</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_all</span><span class="token punctuation">(</span>self<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> Any<span class="token punctuation">]</span><span class="token punctuation">:</span>
|
|
<span class="token triple-quoted-string string">"""
|
|
获取所有键值对
|
|
Returns:
|
|
dict[str, Any]: 键值对
|
|
"""</span>
|
|
<span class="token keyword">if</span> IS_MAIN_PROCESS<span class="token punctuation">:</span>
|
|
<span class="token keyword">return</span> self<span class="token punctuation">.</span>_store
|
|
<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> 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>
|
|
self<span class="token punctuation">.</span>passive_chan<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">'get_all'</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 class="line-number"></div></div></div></details><h3 id="method-publish-self-channel-str-data-any-none" tabindex="-1"><a class="header-anchor" href="#method-publish-self-channel-str-data-any-none"><span><em>method</em> <code>publish(self, channel_: str, data: Any) -> None</code></span></a></h3><p><strong>说明</strong>: 发布消息</p><p><strong>参数</strong>:</p><blockquote><ul><li>channel_: 频道</li><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">publish</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> channel_<span class="token punctuation">:</span> <span class="token builtin">str</span><span class="token punctuation">,</span> data<span class="token punctuation">:</span> Any<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> <span class="token boolean">None</span><span class="token punctuation">:</span>
|
|
<span class="token triple-quoted-string string">"""
|
|
发布消息
|
|
Args:
|
|
channel_: 频道
|
|
data: 数据
|
|
|
|
Returns:
|
|
"""</span>
|
|
self<span class="token punctuation">.</span>active_chan<span class="token punctuation">.</span>send<span class="token punctuation">(</span><span class="token punctuation">(</span><span class="token string">'publish'</span><span class="token punctuation">,</span> <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 string">'data'</span><span class="token punctuation">:</span> data<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></div></details><h3 id="method-on-subscriber-receive-self-channel-str-callable-on-receive-func-on-receive-func" tabindex="-1"><a class="header-anchor" href="#method-on-subscriber-receive-self-channel-str-callable-on-receive-func-on-receive-func"><span><em>method</em> <code>on_subscriber_receive(self, channel_: str) -> Callable[[ON_RECEIVE_FUNC], ON_RECEIVE_FUNC]</code></span></a></h3><p><strong>说明</strong>: 订阅者接收消息时的回调</p><p><strong>参数</strong>:</p><blockquote><ul><li>channel_: 频道</li></ul></blockquote><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">on_subscriber_receive</span><span class="token punctuation">(</span>self<span class="token punctuation">,</span> channel_<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> Callable<span class="token punctuation">[</span><span class="token punctuation">[</span>ON_RECEIVE_FUNC<span class="token punctuation">]</span><span class="token punctuation">,</span> ON_RECEIVE_FUNC<span class="token punctuation">]</span><span class="token punctuation">:</span>
|
|
<span class="token triple-quoted-string string">"""
|
|
订阅者接收消息时的回调
|
|
Args:
|
|
channel_: 频道
|
|
|
|
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">'Cannot subscribe in sub 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> ON_RECEIVE_FUNC<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span> ON_RECEIVE_FUNC<span class="token punctuation">:</span>
|
|
|
|
<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> Any<span class="token punctuation">)</span><span class="token punctuation">:</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">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>
|
|
func<span class="token punctuation">(</span>data<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> channel_ <span class="token keyword">not</span> <span class="token keyword">in</span> _on_main_subscriber_receive_funcs<span class="token punctuation">:</span>
|
|
_on_main_subscriber_receive_funcs<span class="token punctuation">[</span>channel_<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
|
|
_on_main_subscriber_receive_funcs<span class="token punctuation">[</span>channel_<span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>wrapper<span class="token punctuation">)</span>
|
|
<span class="token keyword">else</span><span class="token punctuation">:</span>
|
|
<span class="token keyword">if</span> channel_ <span class="token keyword">not</span> <span class="token keyword">in</span> _on_sub_subscriber_receive_funcs<span class="token punctuation">:</span>
|
|
_on_sub_subscriber_receive_funcs<span class="token punctuation">[</span>channel_<span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span>
|
|
_on_sub_subscriber_receive_funcs<span class="token punctuation">[</span>channel_<span class="token punctuation">]</span><span class="token punctuation">.</span>append<span class="token punctuation">(</span>wrapper<span class="token punctuation">)</span>
|
|
<span class="token keyword">return</span> wrapper
|
|
<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></div></details><h3 id="staticmethod" tabindex="-1"><a class="header-anchor" href="#staticmethod"><span><code>@staticmethod</code></span></a></h3><h3 id="async-method-run-subscriber-receive-funcs-channel-str-data-any" tabindex="-1"><a class="header-anchor" href="#async-method-run-subscriber-receive-funcs-channel-str-data-any"><span><em>async method</em> <code>run_subscriber_receive_funcs(channel_: str, data: Any)</code></span></a></h3><p><strong>说明</strong>: 运行订阅者接收函数</p><p><strong>参数</strong>:</p><blockquote><ul><li>channel_: 频道</li><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 decorator annotation punctuation">@staticmethod</span>
|
|
<span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">run_subscriber_receive_funcs</span><span class="token punctuation">(</span>channel_<span class="token punctuation">:</span> <span class="token builtin">str</span><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:
|
|
channel_: 频道
|
|
data: 数据
|
|
"""</span>
|
|
<span class="token punctuation">[</span>asyncio<span class="token punctuation">.</span>create_task<span class="token punctuation">(</span>func<span class="token punctuation">(</span>data<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token keyword">for</span> func <span class="token keyword">in</span> _on_main_subscriber_receive_funcs<span class="token punctuation">[</span>channel_<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></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 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">'Cannot start receive loop in sub process.'</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> <span class="token keyword">await</span> self<span class="token punctuation">.</span>active_chan<span class="token punctuation">.</span>async_receive<span class="token punctuation">(</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">if</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">'publish'</span><span class="token punctuation">:</span>
|
|
<span class="token keyword">await</span> self<span class="token punctuation">.</span>run_subscriber_receive_funcs<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> 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">'data'</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
|
|
self<span class="token punctuation">.</span>publish_channel<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></div></details><h3 id="class-globalkeyvaluestore" tabindex="-1"><a class="header-anchor" href="#class-globalkeyvaluestore"><span><strong>class</strong> <code>GlobalKeyValueStore</code></span></a></h3><h3 id="classmethod" tabindex="-1"><a class="header-anchor" href="#classmethod"><span><code>@classmethod</code></span></a></h3><h3 id="method-get-instance-cls" tabindex="-1"><a class="header-anchor" href="#method-get-instance-cls"><span><em>method</em> <code>get_instance(cls)</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">@classmethod</span>
|
|
<span class="token keyword">def</span> <span class="token function">get_instance</span><span class="token punctuation">(</span>cls<span class="token punctuation">)</span><span class="token punctuation">:</span>
|
|
<span class="token keyword">if</span> cls<span class="token punctuation">.</span>_instance <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
|
|
<span class="token keyword">with</span> cls<span class="token punctuation">.</span>_lock<span class="token punctuation">:</span>
|
|
<span class="token keyword">if</span> cls<span class="token punctuation">.</span>_instance <span class="token keyword">is</span> <span class="token boolean">None</span><span class="token punctuation">:</span>
|
|
cls<span class="token punctuation">.</span>_instance <span class="token operator">=</span> KeyValueStore<span class="token punctuation">(</span><span class="token punctuation">)</span>
|
|
<span class="token keyword">return</span> cls<span class="token punctuation">.</span>_instance
|
|
</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></div></details><h3 id="var-on-main-subscriber-receive-funcs" tabindex="-1"><a class="header-anchor" href="#var-on-main-subscriber-receive-funcs"><span><em><strong>var</strong></em> <code>_on_main_subscriber_receive_funcs = {}</code></span></a></h3><ul><li><p><strong>类型</strong>: <code>dict[str, list[ASYNC_ON_RECEIVE_FUNC]]</code></p></li><li><p><strong>说明</strong>: 主进程订阅者接收函数</p></li></ul><h3 id="var-on-sub-subscriber-receive-funcs" tabindex="-1"><a class="header-anchor" href="#var-on-sub-subscriber-receive-funcs"><span><em><strong>var</strong></em> <code>_on_sub_subscriber_receive_funcs = {}</code></span></a></h3><ul><li><p><strong>类型</strong>: <code>dict[str, list[ASYNC_ON_RECEIVE_FUNC]]</code></p></li><li><p><strong>说明</strong>: 子进程订阅者接收函数</p></li></ul><h3 id="var-shared-memory-globalkeyvaluestore-get-instance" tabindex="-1"><a class="header-anchor" href="#var-shared-memory-globalkeyvaluestore-get-instance"><span><em><strong>var</strong></em> <code>shared_memory = GlobalKeyValueStore.get_instance()</code></span></a></h3><ul><li><strong>类型</strong>: <code>KeyValueStore</code></li></ul>`,65),o=[p];function c(l,i){return s(),a("div",null,o)}const r=n(t,[["render",c],["__file","storage.html.vue"]]),d=JSON.parse(`{"path":"/dev/api/comm/storage.html","title":"liteyuki.comm.storage","lang":"zh-CN","frontmatter":{"title":"liteyuki.comm.storage","description":"@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'get') func on_get() 源代码 @shared_memory.passive_chan.on_receive(lambda d: d[0] == 'set') func on_set() 源代码 @shared_memor...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/api/comm/storage.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/api/comm/storage.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot 轻雪机器人"}],["meta",{"property":"og:title","content":"liteyuki.comm.storage"}],["meta",{"property":"og:description","content":"@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'get') func on_get() 源代码 @shared_memory.passive_chan.on_receive(lambda d: d[0] == 'set') func on_set() 源代码 @shared_memor..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:locale:alternate","content":"en-US"}],["meta",{"property":"og:updated_time","content":"2024-08-29T06:19:39.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-29T06:19:39.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"liteyuki.comm.storage\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-29T06:19:39.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":3,"title":"@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'get')","slug":"shared-memory-passive-chan-on-receive-lambda-d-d-0-get","link":"#shared-memory-passive-chan-on-receive-lambda-d-d-0-get","children":[]},{"level":3,"title":"func on_get()","slug":"func-on-get","link":"#func-on-get","children":[]},{"level":3,"title":"@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'set')","slug":"shared-memory-passive-chan-on-receive-lambda-d-d-0-set","link":"#shared-memory-passive-chan-on-receive-lambda-d-d-0-set","children":[]},{"level":3,"title":"func on_set()","slug":"func-on-set","link":"#func-on-set","children":[]},{"level":3,"title":"@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'delete')","slug":"shared-memory-passive-chan-on-receive-lambda-d-d-0-delete","link":"#shared-memory-passive-chan-on-receive-lambda-d-d-0-delete","children":[]},{"level":3,"title":"func on_delete()","slug":"func-on-delete","link":"#func-on-delete","children":[]},{"level":3,"title":"@shared_memory.passive_chan.on_receive(lambda d: d[0] == 'get_all')","slug":"shared-memory-passive-chan-on-receive-lambda-d-d-0-get-all","link":"#shared-memory-passive-chan-on-receive-lambda-d-d-0-get-all","children":[]},{"level":3,"title":"func on_get_all()","slug":"func-on-get-all","link":"#func-on-get-all","children":[]},{"level":3,"title":"class KeyValueStore","slug":"class-keyvaluestore","link":"#class-keyvaluestore","children":[]},{"level":3,"title":"method __init__(self)","slug":"method-init-self","link":"#method-init-self","children":[]},{"level":3,"title":"method set(self, key: str, value: Any) -> None","slug":"method-set-self-key-str-value-any-none","link":"#method-set-self-key-str-value-any-none","children":[]},{"level":3,"title":"method get(self, key: str, default: Optional[Any] = None) -> Optional[Any]","slug":"method-get-self-key-str-default-optional-any-none-optional-any","link":"#method-get-self-key-str-default-optional-any-none-optional-any","children":[]},{"level":3,"title":"method delete(self, key: str, ignore_key_error: bool = True) -> None","slug":"method-delete-self-key-str-ignore-key-error-bool-true-none","link":"#method-delete-self-key-str-ignore-key-error-bool-true-none","children":[]},{"level":3,"title":"method get_all(self) -> dict[str, Any]","slug":"method-get-all-self-dict-str-any","link":"#method-get-all-self-dict-str-any","children":[]},{"level":3,"title":"method publish(self, channel_: str, data: Any) -> None","slug":"method-publish-self-channel-str-data-any-none","link":"#method-publish-self-channel-str-data-any-none","children":[]},{"level":3,"title":"method on_subscriber_receive(self, channel_: str) -> Callable[[ON_RECEIVE_FUNC], ON_RECEIVE_FUNC]","slug":"method-on-subscriber-receive-self-channel-str-callable-on-receive-func-on-receive-func","link":"#method-on-subscriber-receive-self-channel-str-callable-on-receive-func-on-receive-func","children":[]},{"level":3,"title":"@staticmethod","slug":"staticmethod","link":"#staticmethod","children":[]},{"level":3,"title":"async method run_subscriber_receive_funcs(channel_: str, data: Any)","slug":"async-method-run-subscriber-receive-funcs-channel-str-data-any","link":"#async-method-run-subscriber-receive-funcs-channel-str-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":"class GlobalKeyValueStore","slug":"class-globalkeyvaluestore","link":"#class-globalkeyvaluestore","children":[]},{"level":3,"title":"@classmethod","slug":"classmethod","link":"#classmethod","children":[]},{"level":3,"title":"method get_instance(cls)","slug":"method-get-instance-cls","link":"#method-get-instance-cls","children":[]},{"level":3,"title":"var _on_main_subscriber_receive_funcs = {}","slug":"var-on-main-subscriber-receive-funcs","link":"#var-on-main-subscriber-receive-funcs","children":[]},{"level":3,"title":"var _on_sub_subscriber_receive_funcs = {}","slug":"var-on-sub-subscriber-receive-funcs","link":"#var-on-sub-subscriber-receive-funcs","children":[]},{"level":3,"title":"var shared_memory = GlobalKeyValueStore.get_instance()","slug":"var-shared-memory-globalkeyvaluestore-get-instance","link":"#var-shared-memory-globalkeyvaluestore-get-instance","children":[]}],"git":{"createdTime":1724031826000,"updatedTime":1724912379000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":6}]},"readingTime":{"minutes":3.34,"words":1003},"filePathRelative":"dev/api/comm/storage.md","localizedDate":"2024年8月19日","autoDesc":true}`);export{r as comp,d as data};
|