File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change 1+ import asyncio
2+ import concurrent
3+ from typing import Any , Coroutine
4+
5+
6+ def run_async (async_function : Coroutine [Any , Any , Any ]) -> Any :
7+ try :
8+ _ = asyncio .get_running_loop ()
9+ except RuntimeError :
10+ return asyncio .run (async_function )
11+
12+ with concurrent .futures .ThreadPoolExecutor () as executor :
13+ return executor .submit (asyncio .run , async_function ).result ()
Original file line number Diff line number Diff line change 1+ import asyncio
2+
3+ from qi2_shared .utils import run_async
4+
5+
6+ def test_async_run_no_loop () -> None :
7+ async def t_coro () -> None :
8+ await asyncio .sleep (1 )
9+
10+ run_async (t_coro ())
11+
12+
13+ def test_async_run_loop () -> None :
14+ async def t_coro () -> None :
15+ await asyncio .sleep (1 )
16+
17+ async def main () -> None :
18+ run_async (t_coro ())
19+
20+ asyncio .run (main ())
You can’t perform that action at this time.
0 commit comments