mirror of
https://github.com/aljazceru/nutshell.git
synced 2025-12-20 10:34:20 +01:00
initial commit
This commit is contained in:
26
core/helpers.py
Normal file
26
core/helpers.py
Normal file
@@ -0,0 +1,26 @@
|
||||
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]
|
||||
Reference in New Issue
Block a user