misc.py 610 B

123456789101112131415161718192021
  1. """Miscellaneous functions for the experimental package."""
  2. import asyncio
  3. from typing import Any
  4. async def run_in_thread(func) -> 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.background() decorated method.
  7. Args:
  8. func (callable): The non-async function to run.
  9. Returns:
  10. Any: The return value of the function.
  11. """
  12. assert not asyncio.coroutines.iscoroutinefunction(
  13. func
  14. ), "func must be a non-async function"
  15. return await asyncio.get_event_loop().run_in_executor(None, func)