import{_ as s,c as i,o as a,a9 as n}from"./chunks/framework.C4_mTacX.js";const g=JSON.parse('{"title":"liteyuki.utils","description":"","frontmatter":{"title":"liteyuki.utils"},"headers":[],"relativePath":"en/dev/api/utils.md","filePath":"en/dev/api/utils.md","lastUpdated":1725101868000}'),t={name:"en/dev/api/utils.md"},l=n(`

Module liteyuki.utils

一些常用的工具类,部分来源于 nonebot 并遵循其许可进行修改


func is_coroutine_callable(call: Callable[..., Any]) -> bool

Description: 判断是否为协程可调用对象

Arguments:

Return: bool: 是否为协程可调用对象

Source code or View on GitHub
python
def is_coroutine_callable(call: Callable[..., Any]) -> bool:
    if inspect.isroutine(call):
        return inspect.iscoroutinefunction(call)
    if inspect.isclass(call):
        return False
    func_ = getattr(call, '__call__', None)
    return inspect.iscoroutinefunction(func_)

func run_coroutine(*coro: Coroutine)

Description: 运行协程

Arguments:

Source code or View on GitHub
python
def run_coroutine(*coro: Coroutine):
    try:
        loop = asyncio.get_running_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}')

func run_coroutine_in_thread(*coro: Coroutine)

Description: 在新线程中运行协程

Arguments:

Source code or View on GitHub
python
def run_coroutine_in_thread(*coro: Coroutine):
    threading.Thread(target=run_coroutine, args=coro, daemon=True).start()

func path_to_module_name(path: Path) -> str

Description: 转换路径为模块名

Arguments:

Return: str: 模块名

Source code or View on GitHub
python
def path_to_module_name(path: Path) -> 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,))

func async_wrapper(func: Callable[..., Any]) -> Callable[..., Coroutine]

Description: 异步包装器

Arguments:

Return: Coroutine: Asynchronous Callable

Source code or View on GitHub
python
def async_wrapper(func: Callable[..., Any]) -> Callable[..., Coroutine]:

    async def wrapper(*args, **kwargs):
        return func(*args, **kwargs)
    wrapper.__signature__ = inspect.signature(func)
    return wrapper
`,35),e=[l];function h(p,k,r,o,d,c){return a(),i("div",null,e)}const u=s(t,[["render",h]]);export{g as __pageData,u as default};