use_task
A hook that runs a function or coroutine function as a task and returns the result.
def use_task(
f: Union[None, Callable[P, R]] = None,
*,
dependencies: Union[None, List] = [],
raise_error=True,
prefer_threaded=True,
) -> Union[Callable[[Callable[P, R]], Task[P, R]], Task[P, R]]:
...
Allows you to run code in the background, with the UI available to the user. This is useful for long running tasks, like downloading data or processing data.
Unlike with the @task
decorator, the result is not globally shared, but only available to the component that called use_task
.
Note that unlike the @task
decorator, the task is invoked immediately when dependencies are passed. To prevent this, pass dependencies=None
.
Example
Running in a thread
import time
import solara
from solara.lab import use_task, Task
@solara.component
def Page():
number = solara.use_reactive(4)
def square():
time.sleep(1)
return number.value**2
result: Task[int] = use_task(square, dependencies=[number.value])
solara.InputInt("Square", value=number, continuous_update=True)
if result.finished:
solara.Success(f"Square of {number} == {result.value}")
solara.ProgressLinear(result.pending)
Square of 4 == 16
Live output
Running in an asyncio task
Note that the only difference is our function is now a coroutine function,
and we use asyncio.sleep
instead of time.sleep
.
import asyncio
import solara
from solara.lab import use_task, Task
@solara.component
def Page():
number = solara.use_reactive(4)
async def square():
await asyncio.sleep(1)
return number.value**2
result: Task[int] = use_task(square, dependencies=[number.value])
solara.InputInt("Square", value=number, continuous_update=True)
if result.finished:
solara.Success(f"Square of {number} == {result.value}")
solara.ProgressLinear(result.pending)
Traceback (most recent call last): File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/reacton/core.py", line 1902, in _reconsolidate effect() File "/opt/hostedtoolcache/Python/3.8.18/x64/lib/python3.8/site-packages/reacton/core.py", line 1136, in __call__ self._cleanup = self.callable() File "/home/runner/work/solara/solara/solara/tasks.py", line 834, in run task_instance() File "/home/runner/work/solara/solara/solara/tasks.py", line 430, in __call__ return self._instance.value(*args, **kwargs) File "/home/runner/work/solara/solara/solara/tasks.py", line 194, in __call__ self.current_future = future = asyncio.Future[R]() TypeError: 'type' object is not subscriptable
Live output
Arguments
f
: The function or coroutine to run as a task.dependencies
: A list of dependencies that will trigger a rerun of the task when changed, the task will run automatically execute when thedependencies=None
raise_error
: If true, an error in the task will be raised. If false, the error should be handled by the user and is available in the.exception
attribute of the task result object.prefer_threaded
- bool: Will run coroutine functions as a task in a thread when threads are available. This ensures that even when a coroutine functions calls a blocking function the UI is still responsive. On platform where threads are not supported (like Pyodide / WASM / Emscripten / PyScript), a coroutine function will always run in the current event loop.