This commit is contained in:
yumoqing 2024-08-07 11:33:54 +08:00
parent 54a11439b6
commit 4b1fd99738

View File

@ -8,9 +8,16 @@ from functools import wraps
def awaitify(sync_func): def awaitify(sync_func):
"""Wrap a synchronous callable to allow ``await``'ing it""" """Wrap a synchronous callable to allow ``await``'ing it"""
@wraps(sync_func) @wraps(sync_func)
async def async_func(*args, **kwargs): async def async_func(*args, **kw):
loop = asyncio.get_event_loop() loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, sync_func, *args, **kwargs) return await loop.run_in_executor(None, sync_func, *args, **kw)
return async_func
def coroutinify(func):
@wraps(sync_func)
async def async_func(*args):
loop = asyncio.get_event_loop()
return await loop.run_in_executor(None, sync_func, *args)
return async_func return async_func
def to_func(func): def to_func(func):