mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-20 10:34:20 +01:00
27 lines
661 B
Python
27 lines
661 B
Python
import asyncio
|
|
from functools import partial, wraps
|
|
|
|
|
|
def async_wrap(func):
|
|
@wraps(func)
|
|
async def run(*args, loop=None, executor=None, **kwargs):
|
|
if loop is None:
|
|
loop = asyncio.get_event_loop()
|
|
partial_func = partial(func, *args, **kwargs)
|
|
return await loop.run_in_executor(executor, partial_func)
|
|
|
|
return run
|
|
|
|
|
|
def async_unwrap(to_await):
|
|
async_response = []
|
|
|
|
async def run_and_capture_result():
|
|
r = await to_await
|
|
async_response.append(r)
|
|
|
|
loop = asyncio.get_event_loop()
|
|
coroutine = run_and_capture_result()
|
|
loop.run_until_complete(coroutine)
|
|
return async_response[0]
|