Utility Functions

Reflex provides utility functions to help with common tasks in your applications.

run_in_thread

The run_in_thread function allows you to run a non-async function in a separate thread, which is useful for preventing long-running operations from blocking the UI event queue.

Parameters

  • func: The non-async function to run in a separate thread.

Returns

  • The return value of the function.

Raises

  • ValueError: If the function is an async function.

Usage

run_in_thread Example

When to Use run_in_thread

Use run_in_thread when you need to:

  1. Execute CPU-bound operations that would otherwise block the event loop
  2. Call synchronous libraries that don't have async equivalents
  3. Prevent long-running operations from blocking UI responsiveness

Example: Processing a Large File

Built with Reflex