allow return from run_in_thread (#3477)
This commit is contained in:
parent
42f34f763e
commit
1c8bebcaf6
@ -1,12 +1,21 @@
|
|||||||
"""Miscellaneous functions for the experimental package."""
|
"""Miscellaneous functions for the experimental package."""
|
||||||
|
|
||||||
import asyncio
|
import asyncio
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
|
||||||
async def run_in_thread(func):
|
async def run_in_thread(func) -> Any:
|
||||||
"""Run a function in a separate thread.
|
"""Run a function in a separate thread.
|
||||||
|
|
||||||
|
To not block the UI event queue, run_in_thread must be inside inside a rx.background() decorated method.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
func (callable): The function to run.
|
func (callable): The non-async function to run.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Any: The return value of the function.
|
||||||
"""
|
"""
|
||||||
await asyncio.get_event_loop().run_in_executor(None, func)
|
assert not asyncio.coroutines.iscoroutinefunction(
|
||||||
|
func
|
||||||
|
), "func must be a non-async function"
|
||||||
|
return await asyncio.get_event_loop().run_in_executor(None, func)
|
||||||
|
Loading…
Reference in New Issue
Block a user