19 lines
417 B
Python
19 lines
417 B
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import asyncio
|
||
|
|
from concurrent.futures import ThreadPoolExecutor
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
_executor = ThreadPoolExecutor(max_workers=4)
|
||
|
|
|
||
|
|
|
||
|
|
def run_async(coro: Any, timeout: int = 30):
|
||
|
|
try:
|
||
|
|
asyncio.get_running_loop()
|
||
|
|
except RuntimeError:
|
||
|
|
return asyncio.run(coro)
|
||
|
|
return _executor.submit(asyncio.run, coro).result(timeout=timeout)
|
||
|
|
|
||
|
|
|
||
|
|
__all__ = ["run_async"]
|