import{_ as n,o as a,c as s,e}from"./app-BQBtFyJe.js";const t={},o=e(`
判断是否为协程可调用对象
Args:
call: 可调用对象
Returns:
bool: 是否为协程可调用对象
源代码
def is_coroutine_callable(call: Callable[..., Any]) -> bool:
"""
判断是否为协程可调用对象
Args:
call: 可调用对象
Returns:
bool: 是否为协程可调用对象
"""
if inspect.isroutine(call):
return inspect.iscoroutinefunction(call)
if inspect.isclass(call):
return False
func_ = getattr(call, '__call__', None)
return inspect.iscoroutinefunction(func_)
运行协程
Args:
coro:
Returns:
源代码
def run_coroutine(*coro: Coroutine):
"""
运行协程
Args:
coro:
Returns:
"""
try:
loop = asyncio.get_event_loop()
if loop.is_running():
for c in coro:
asyncio.ensure_future(c)
else:
for c in coro:
loop.run_until_complete(c)
except RuntimeError:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(asyncio.gather(*coro))
loop.close()
except Exception as e:
logger.error(f'Exception occurred: {e}')
在新线程中运行协程
Args:
coro:
Returns:
源代码
def run_coroutine_in_thread(*coro: Coroutine):
"""
在新线程中运行协程
Args:
coro:
Returns:
"""
threading.Thread(target=run_coroutine, args=coro, daemon=True).start()
转换路径为模块名
Args:
path: 路径a/b/c/d -> a.b.c.d
Returns:
str: 模块名
源代码
def path_to_module_name(path: Path) -> str:
"""
转换路径为模块名
Args:
path: 路径a/b/c/d -> a.b.c.d
Returns:
str: 模块名
"""
rel_path = path.resolve().relative_to(Path.cwd().resolve())
if rel_path.stem == '__init__':
return '.'.join(rel_path.parts[:-1])
else:
return '.'.join(rel_path.parts[:-1] + (rel_path.stem,))
异步包装器
Args:
func: Sync Callable
Returns:
Coroutine: Asynchronous Callable
源代码
def async_wrapper(func: Callable[..., Any]) -> Callable[..., Coroutine]:
"""
异步包装器
Args:
func: Sync Callable
Returns:
Coroutine: Asynchronous Callable
"""
async def wrapper(*args, **kwargs):
return func(*args, **kwargs)
wrapper.__signature__ = inspect.signature(func)
return wrapper
源代码
async def wrapper(*args, **kwargs):
return func(*args, **kwargs)
`,40),p=[o];function l(c,i){return a(),s("div",null,p)}const u=n(t,[["render",l],["__file","utils.html.vue"]]),d=JSON.parse(`{"path":"/en/dev/api/utils.html","title":"liteyuki.utils","lang":"en-US","frontmatter":{"title":"liteyuki.utils","order":1,"icon":"laptop-code","category":"API","description":"def is_coroutine_callable(call: Callable[..., Any]) -> bool 判断是否为协程可调用对象 Args: Returns: 源代码 def run_coroutine() -> None 运行协程 Args: Returns: 源代码 def run_coroutine_in_thread() -> ...","head":[["link",{"rel":"alternate","hreflang":"zh-cn","href":"https://vuepress-theme-hope-docs-demo.netlify.app/dev/api/utils.html"}],["meta",{"property":"og:url","content":"https://vuepress-theme-hope-docs-demo.netlify.app/en/dev/api/utils.html"}],["meta",{"property":"og:site_name","content":"LiteyukiBot"}],["meta",{"property":"og:title","content":"liteyuki.utils"}],["meta",{"property":"og:description","content":"def is_coroutine_callable(call: Callable[..., Any]) -> bool 判断是否为协程可调用对象 Args: Returns: 源代码 def run_coroutine() -> None 运行协程 Args: Returns: 源代码 def run_coroutine_in_thread() -> ..."}],["meta",{"property":"og:type","content":"article"}],["meta",{"property":"og:locale","content":"en-US"}],["meta",{"property":"og:locale:alternate","content":"zh-CN"}],["meta",{"property":"og:updated_time","content":"2024-08-21T09:59:21.000Z"}],["meta",{"property":"article:modified_time","content":"2024-08-21T09:59:21.000Z"}],["script",{"type":"application/ld+json"},"{\\"@context\\":\\"https://schema.org\\",\\"@type\\":\\"Article\\",\\"headline\\":\\"liteyuki.utils\\",\\"image\\":[\\"\\"],\\"dateModified\\":\\"2024-08-21T09:59:21.000Z\\",\\"author\\":[]}"]]},"headers":[{"level":3,"title":"def is_coroutine_callable(call: Callable[..., Any]) -> bool","slug":"def-is-coroutine-callable-call-callable-any-bool","link":"#def-is-coroutine-callable-call-callable-any-bool","children":[]},{"level":3,"title":"def run_coroutine() -> None","slug":"def-run-coroutine-none","link":"#def-run-coroutine-none","children":[]},{"level":3,"title":"def run_coroutine_in_thread() -> None","slug":"def-run-coroutine-in-thread-none","link":"#def-run-coroutine-in-thread-none","children":[]},{"level":3,"title":"def path_to_module_name(path: Path) -> str","slug":"def-path-to-module-name-path-path-str","link":"#def-path-to-module-name-path-path-str","children":[]},{"level":3,"title":"def async_wrapper(func: Callable[..., Any]) -> Callable[..., Coroutine]","slug":"def-async-wrapper-func-callable-any-callable-coroutine","link":"#def-async-wrapper-func-callable-any-callable-coroutine","children":[]},{"level":3,"title":"async def wrapper() -> None","slug":"async-def-wrapper-none","link":"#async-def-wrapper-none","children":[]},{"level":3,"title":"var IS_MAIN_PROCESS = multiprocessing.current_process().name == 'MainProcess'","slug":"var-is-main-process-multiprocessing-current-process-name-mainprocess","link":"#var-is-main-process-multiprocessing-current-process-name-mainprocess","children":[]},{"level":3,"title":"var func_ = getattr(call, '__call__', None)","slug":"var-func-getattr-call-call-none","link":"#var-func-getattr-call-call-none","children":[]},{"level":3,"title":"var rel_path = path.resolve().relative_to(Path.cwd().resolve())","slug":"var-rel-path-path-resolve-relative-to-path-cwd-resolve","link":"#var-rel-path-path-resolve-relative-to-path-cwd-resolve","children":[]},{"level":3,"title":"var loop = asyncio.get_event_loop()","slug":"var-loop-asyncio-get-event-loop","link":"#var-loop-asyncio-get-event-loop","children":[]},{"level":3,"title":"var loop = asyncio.new_event_loop()","slug":"var-loop-asyncio-new-event-loop","link":"#var-loop-asyncio-new-event-loop","children":[]}],"git":{"createdTime":1724234361000,"updatedTime":1724234361000,"contributors":[{"name":"snowy","email":"snowykami@outlook.com","commits":1}]},"readingTime":{"minutes":1.37,"words":411},"filePathRelative":"en/dev/api/utils.md","localizedDate":"August 21, 2024","autoDesc":true}`);export{u as comp,d as data};