1
0

misc.py 731 B

123456789101112131415161718192021222324
  1. """Miscellaneous functions for the experimental package."""
  2. import asyncio
  3. from collections.abc import Callable
  4. from typing import Any
  5. async def run_in_thread(func: Callable) -> Any:
  6. """Run a function in a separate thread.
  7. To not block the UI event queue, run_in_thread must be inside inside a rx.event(background=True) decorated method.
  8. Args:
  9. func: The non-async function to run.
  10. Raises:
  11. ValueError: If the function is an async function.
  12. Returns:
  13. Any: The return value of the function.
  14. """
  15. if asyncio.coroutines.iscoroutinefunction(func):
  16. raise ValueError("func must be a non-async function")
  17. return await asyncio.get_event_loop().run_in_executor(None, func)