1
0
forked from bot/app
LiteyukiBot/assets/utils.html-BAP87kMl.js

76 lines
18 KiB
JavaScript

import{_ as n,o as s,c as a,d as t}from"./app-CLi4lRj1.js";const e={},o=t(`<h3 id="func-is-coroutine-callable-bool" tabindex="-1"><a class="header-anchor" href="#func-is-coroutine-callable-bool"><span><em>func</em> <code>is_coroutine_callable() -&gt; bool</code></span></a></h3><p><strong>Description</strong>: 判断是否为协程可调用对象</p><p><strong>Return</strong>: bool: 是否为协程可调用对象</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>call: 可调用对象</li></ul></blockquote><details><summary><b>Source code</b></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">is_coroutine_callable</span><span class="token punctuation">(</span>call<span class="token punctuation">:</span> Callable<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> Any<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token builtin">bool</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
判断是否为协程可调用对象
Args:
call: 可调用对象
Returns:
bool: 是否为协程可调用对象
&quot;&quot;&quot;</span>
<span class="token keyword">if</span> inspect<span class="token punctuation">.</span>isroutine<span class="token punctuation">(</span>call<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> inspect<span class="token punctuation">.</span>iscoroutinefunction<span class="token punctuation">(</span>call<span class="token punctuation">)</span>
<span class="token keyword">if</span> inspect<span class="token punctuation">.</span>isclass<span class="token punctuation">(</span>call<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token boolean">False</span>
func_ <span class="token operator">=</span> <span class="token builtin">getattr</span><span class="token punctuation">(</span>call<span class="token punctuation">,</span> <span class="token string">&#39;__call__&#39;</span><span class="token punctuation">,</span> <span class="token boolean">None</span><span class="token punctuation">)</span>
<span class="token keyword">return</span> inspect<span class="token punctuation">.</span>iscoroutinefunction<span class="token punctuation">(</span>func_<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="func-run-coroutine" tabindex="-1"><a class="header-anchor" href="#func-run-coroutine"><span><em>func</em> <code>run_coroutine()</code></span></a></h3><p><strong>Description</strong>: 运行协程</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>coro:</li></ul></blockquote><details><summary><b>Source code</b></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">run_coroutine</span><span class="token punctuation">(</span><span class="token operator">*</span>coro<span class="token punctuation">:</span> Coroutine<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
运行协程
Args:
coro:
Returns:
&quot;&quot;&quot;</span>
<span class="token keyword">try</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>
<span class="token keyword">if</span> loop<span class="token punctuation">.</span>is_running<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">for</span> c <span class="token keyword">in</span> coro<span class="token punctuation">:</span>
asyncio<span class="token punctuation">.</span>ensure_future<span class="token punctuation">(</span>c<span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">for</span> c <span class="token keyword">in</span> coro<span class="token punctuation">:</span>
loop<span class="token punctuation">.</span>run_until_complete<span class="token punctuation">(</span>c<span class="token punctuation">)</span>
<span class="token keyword">except</span> RuntimeError<span class="token punctuation">:</span>
loop <span class="token operator">=</span> asyncio<span class="token punctuation">.</span>new_event_loop<span class="token punctuation">(</span><span class="token punctuation">)</span>
asyncio<span class="token punctuation">.</span>set_event_loop<span class="token punctuation">(</span>loop<span class="token punctuation">)</span>
loop<span class="token punctuation">.</span>run_until_complete<span class="token punctuation">(</span>asyncio<span class="token punctuation">.</span>gather<span class="token punctuation">(</span><span class="token operator">*</span>coro<span class="token punctuation">)</span><span class="token punctuation">)</span>
loop<span class="token punctuation">.</span>close<span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token keyword">except</span> Exception <span class="token keyword">as</span> e<span class="token punctuation">:</span>
logger<span class="token punctuation">.</span>error<span class="token punctuation">(</span><span class="token string-interpolation"><span class="token string">f&#39;Exception occurred: </span><span class="token interpolation"><span class="token punctuation">{</span>e<span class="token punctuation">}</span></span><span class="token string">&#39;</span></span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div></details><h3 id="func-run-coroutine-in-thread" tabindex="-1"><a class="header-anchor" href="#func-run-coroutine-in-thread"><span><em>func</em> <code>run_coroutine_in_thread()</code></span></a></h3><p><strong>Description</strong>: 在新线程中运行协程</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>coro:</li></ul></blockquote><details><summary><b>Source code</b></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">run_coroutine_in_thread</span><span class="token punctuation">(</span><span class="token operator">*</span>coro<span class="token punctuation">:</span> Coroutine<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
在新线程中运行协程
Args:
coro:
Returns:
&quot;&quot;&quot;</span>
threading<span class="token punctuation">.</span>Thread<span class="token punctuation">(</span>target<span class="token operator">=</span>run_coroutine<span class="token punctuation">,</span> args<span class="token operator">=</span>coro<span class="token punctuation">,</span> daemon<span class="token operator">=</span><span class="token boolean">True</span><span class="token punctuation">)</span><span class="token punctuation">.</span>start<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="func-path-to-module-name-str" tabindex="-1"><a class="header-anchor" href="#func-path-to-module-name-str"><span><em>func</em> <code>path_to_module_name() -&gt; str</code></span></a></h3><p><strong>Description</strong>: 转换路径为模块名</p><p><strong>Return</strong>: str: 模块名</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>path: 路径a/b/c/d -&gt; a.b.c.d</li></ul></blockquote><details><summary><b>Source code</b></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">path_to_module_name</span><span class="token punctuation">(</span>path<span class="token punctuation">:</span> Path<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> <span class="token builtin">str</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
转换路径为模块名
Args:
path: 路径a/b/c/d -&gt; a.b.c.d
Returns:
str: 模块名
&quot;&quot;&quot;</span>
rel_path <span class="token operator">=</span> path<span class="token punctuation">.</span>resolve<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>relative_to<span class="token punctuation">(</span>Path<span class="token punctuation">.</span>cwd<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>resolve<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token keyword">if</span> rel_path<span class="token punctuation">.</span>stem <span class="token operator">==</span> <span class="token string">&#39;__init__&#39;</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token string">&#39;.&#39;</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>rel_path<span class="token punctuation">.</span>parts<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> <span class="token string">&#39;.&#39;</span><span class="token punctuation">.</span>join<span class="token punctuation">(</span>rel_path<span class="token punctuation">.</span>parts<span class="token punctuation">[</span><span class="token punctuation">:</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">+</span> <span class="token punctuation">(</span>rel_path<span class="token punctuation">.</span>stem<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></div></details><h3 id="func-async-wrapper-callable-coroutine" tabindex="-1"><a class="header-anchor" href="#func-async-wrapper-callable-coroutine"><span><em>func</em> <code>async_wrapper() -&gt; Callable[..., Coroutine]</code></span></a></h3><p><strong>Description</strong>: 异步包装器</p><p><strong>Return</strong>: Coroutine: Asynchronous Callable</p><p><strong>Arguments</strong>:</p><blockquote><ul><li>func: Sync Callable</li></ul></blockquote><details><summary><b>Source code</b></summary><div class="language-python line-numbers-mode" data-ext="py" data-title="py"><pre class="language-python"><code><span class="token keyword">def</span> <span class="token function">async_wrapper</span><span class="token punctuation">(</span>func<span class="token punctuation">:</span> Callable<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> Any<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">&gt;</span> Callable<span class="token punctuation">[</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">,</span> Coroutine<span class="token punctuation">]</span><span class="token punctuation">:</span>
<span class="token triple-quoted-string string">&quot;&quot;&quot;
异步包装器
Args:
func: Sync Callable
Returns:
Coroutine: Asynchronous Callable
&quot;&quot;&quot;</span>
<span class="token keyword">async</span> <span class="token keyword">def</span> <span class="token function">wrapper</span><span class="token punctuation">(</span><span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">**</span>kwargs<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token keyword">return</span> func<span class="token punctuation">(</span><span class="token operator">*</span>args<span class="token punctuation">,</span> <span class="token operator">**</span>kwargs<span class="token punctuation">)</span>
wrapper<span class="token punctuation">.</span>__signature__ <span class="token operator">=</span> inspect<span class="token punctuation">.</span>signature<span class="token punctuation">(</span>func<span class="token punctuation">)</span>
<span class="token keyword">return</span> wrapper
</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>`,28),p=[o];function c(l,i){return s(),a("div",null,p)}const r=n(e,[["render",c],["__file","utils.html.vue"]]),d=JSON.parse('{"path":"/en/api/utils.html","title":"liteyuki.utils","lang":"en-US","frontmatter":{"title":"liteyuki.utils","description":"func is_coroutine_callable() -> bool Description: 判断是否为协程可调用对象 Return: bool: 是否为协程可调用对象 Arguments: call: 可调用对象 Source code func run_coroutine() Description: 运行协程 Arguments: coro...","head":[["link",{"rel":"alternate","hreflang":"zh-cn","href":"https://vuepress-theme-hope-docs-demo.netlify.app/api/utils.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/en/api/utils.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot"}],["meta",{"property":"og:title","content":"liteyuki.utils"}],["meta",{"property":"og:description","content":"func is_coroutine_callable() -> bool Description: 判断是否为协程可调用对象 Return: bool: 是否为协程可调用对象 Arguments: call: 可调用对象 Source code func run_coroutine() Description: 运行协程 Arguments: coro..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"en-US"}],["meta",{"property":"og:locale:alternate","content":"zh-CN"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"liteyuki.utils\\",\\"image\\":[\\"\\"],\\"dateModified\\":null,\\"author\\":[]}"]]},"headers":[{"level":3,"title":"func is_coroutine_callable() -> bool","slug":"func-is-coroutine-callable-bool","link":"#func-is-coroutine-callable-bool","children":[]},{"level":3,"title":"func run_coroutine()","slug":"func-run-coroutine","link":"#func-run-coroutine","children":[]},{"level":3,"title":"func run_coroutine_in_thread()","slug":"func-run-coroutine-in-thread","link":"#func-run-coroutine-in-thread","children":[]},{"level":3,"title":"func path_to_module_name() -> str","slug":"func-path-to-module-name-str","link":"#func-path-to-module-name-str","children":[]},{"level":3,"title":"func async_wrapper() -> Callable[..., Coroutine]","slug":"func-async-wrapper-callable-coroutine","link":"#func-async-wrapper-callable-coroutine","children":[]}],"git":{"createdTime":null,"updatedTime":null,"contributors":[]},"readingTime":{"minutes":1.19,"words":356},"filePathRelative":"en/api/utils.md","autoDesc":true}');export{r as comp,d as data};