misc.py 704 B

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