forked from bot/app
76 lines
18 KiB
JavaScript
76 lines
18 KiB
JavaScript
|
import{_ as n,o as s,c as a,d as t}from"./app-EhCe7g9m.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() -> bool</code></span></a></h3><p><strong>说明</strong>: 判断是否为协程可调用对象</p><p><strong>返回</strong>: bool: 是否为协程可调用对象</p><p><strong>参数</strong>:</p><blockquote><ul><li>call: 可调用对象</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">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">></span> <span class="token builtin">bool</span><span class="token punctuation">:</span>
|
||
|
<span class="token triple-quoted-string string">"""
|
||
|
判断是否为协程可调用对象
|
||
|
Args:
|
||
|
call: 可调用对象
|
||
|
Returns:
|
||
|
bool: 是否为协程可调用对象
|
||
|
"""</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">'__call__'</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>说明</strong>: 运行协程</p><p><strong>参数</strong>:</p><blockquote><ul><li>coro:</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">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">"""
|
||
|
运行协程
|
||
|
Args:
|
||
|
coro:
|
||
|
|
||
|
Returns:
|
||
|
|
||
|
"""</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'Exception occurred: </span><span class="token interpolation"><span class="token punctuation">{</span>e<span class="token punctuation">}</span></span><span class="token string">'</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>说明</strong>: 在新线程中运行协程</p><p><strong>参数</strong>:</p><blockquote><ul><li>coro:</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">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">"""
|
||
|
在新线程中运行协程
|
||
|
Args:
|
||
|
coro:
|
||
|
|
||
|
Returns:
|
||
|
|
||
|
"""</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() -> str</code></span></a></h3><p><strong>说明</strong>: 转换路径为模块名</p><p><strong>返回</strong>: str: 模块名</p><p><strong>参数</strong>:</p><blockquote><ul><li>path: 路径a/b/c/d -> a.b.c.d</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">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">></span> <span class="token builtin">str</span><span class="token punctuation">:</span>
|
||
|
<span class="token triple-quoted-string string">"""
|
||
|
转换路径为模块名
|
||
|
Args:
|
||
|
path: 路径a/b/c/d -> a.b.c.d
|
||
|
Returns:
|
||
|
str: 模块名
|
||
|
"""</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">'__init__'</span><span class="token punctuation">:</span>
|
||
|
<span class="token keyword">return</span> <span class="token string">'.'</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">'.'</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() -> Callable[..., Coroutine]</code></span></a></h3><p><strong>说明</strong>: 异步包装器</p><p><strong>返回</strong>: Coroutine: Asynchronous Callable</p><p><strong>参数</strong>:</p><blockquote><ul><li>func: Sync Callable</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">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">></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">"""
|
||
|
异步包装器
|
||
|
Args:
|
||
|
func: Sync Callable
|
||
|
Returns:
|
||
|
Coroutine: Asynchronous Callable
|
||
|
"""</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":"/api/utils.html","title":"liteyuki.utils","lang":"zh-CN","frontmatter":{"title":"liteyuki.utils","description":"func is_coroutine_callable() -> bool 说明: 判断是否为协程可调用对象 返回: bool: 是否为协程可调用对象 参数: call: 可调用对象 源代码 func run_coroutine() 说明: 运行协程 参数: coro: 源代码 func run_coroutine_in_thread() 说明: 在新线...","head":[["link",{"rel":"alternate","hreflang":"en-us","href":"https://vuepress-theme-hope-docs-demo.netlify.app/en/api/utils.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/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 说明: 判断是否为协程可调用对象 返回: bool: 是否为协程可调用对象 参数: call: 可调用对象 源代码 func run_coroutine() 说明: 运行协程 参数: coro: 源代码 func run_coroutine_in_thread() 说明: 在新线..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"zh-CN"}],["meta",{"property":"og:locale:alternate","content":"en-US"}],["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.25,"words":374},"filePathRelative":"api/utils.md","autoDesc":true}');export{r as comp,d as data};
|