From 1c8bebcaf69d710048f19ba79a4788fb994c8a5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Brand=C3=A9ho?= Date: Thu, 13 Jun 2024 18:59:05 +0200 Subject: [PATCH] allow return from run_in_thread (#3477) --- reflex/experimental/misc.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/reflex/experimental/misc.py b/reflex/experimental/misc.py index fec0bb992..716d081b8 100644 --- a/reflex/experimental/misc.py +++ b/reflex/experimental/misc.py @@ -1,12 +1,21 @@ """Miscellaneous functions for the experimental package.""" 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. + To not block the UI event queue, run_in_thread must be inside inside a rx.background() decorated method. + 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)