Async apps
A Flet app runs on a single asyncio
event loop. Coroutines are supported everywhere a handler is accepted, so you can
use asyncio and any async Python library without wrapping anything.
That single loop is also what sends UI updates to the client, which leads to the one rule that matters most:
While a handler or main() runs, nothing else does - no other events are
dispatched, and no updates reach the screen. A blocking call such as
time.sleep() or requests.get() freezes the UI for as long as it takes.
This applies on every platform, not just the web.
If you are coming from Flet 0.28, where sync handlers ran on a thread pool, read Migrating from Flet 0.28 to 1.0 first - this is the behavior that changed.
Getting started with async
main() can be async and use any asyncio API:
import asyncio
import flet as ft
async def main(page: ft.Page):
await asyncio.sleep(1)
page.add(ft.Text("Hello, async world!"))
ft.run(main)
Use await ft.run_async(main) when your Flet app is part of a larger app and
started from async code.
Control event handlers
Handlers can be sync or async, and Flet picks the right calling convention for each. A sync handler is fine when it does not block:
def handle_resize(e):
print("New page size:", page.window.width, page.window.height)
page.on_resize = handle_resize
Make it async when there is something to await:
async def main(page: ft.Page):
async def handle_click(e):
await some_async_method()
page.add(ft.Text("Hello!"))
page.add(ft.Button(content="Say hello!", on_click=handle_click))
ft.run(main)
A handler can also take no arguments at all, which is convenient when you do not need the event:
def handle_click():
counter.value += 1
ft.Button(content="Add", on_click=handle_click)
Async lambdas
Python has no async lambdas. A lambda handler is fine for simple, non-blocking work:
page.on_error = lambda e: print("Page error:", e.data)
A lambda can call an async function, too. It returns the coroutine, and Flet
awaits it as it would an async def handler, so the automatic update includes
what the coroutine changes:
ft.Button(content="Delete", on_click=lambda: delete_item(item.id))
The same goes for use_effect: ft.use_effect(lambda: load(item_id), [item_id]) runs load as an async setup.
A lambda can also start async work without waiting for it. Handlers run on the event loop, so there is always a running loop to schedule a task on - the lambda kicks the coroutine off instead of awaiting it:
ft.Button(content="Reload", on_click=lambda e: asyncio.create_task(reload(e)))
page.run_task() is usually the better version of this
trick. It takes the coroutine function and its arguments rather than a called
coroutine, keeps a reference to the task so it cannot be garbage-collected
mid-flight, and logs the traceback on the flet logger if the task fails
while nobody is awaiting its future:
ft.Button(content="Reload", on_click=lambda e: page.run_task(reload, e))
Scheduling a task ends the handler immediately, so the automatic update happens
while your coroutine is still running and will not include anything it does. Call
update() yourself for UI changes made inside the task:
async def reload(e):
items.value = await fetch_items()
items.update() # the handler's automatic update already happened
For anything longer than scheduling one call, use an async def handler.
Sleeping
Use asyncio.sleep()
rather than time.sleep(), which blocks the loop:
import asyncio
import flet as ft
def main(page: ft.Page):
async def handle_click(e):
await asyncio.sleep(1)
page.add(ft.Text("Hello!"))
page.add(ft.Button(content="Say hello with delay!", on_click=handle_click))
ft.run(main)
Yielding for intermediate updates
Flet updates the page automatically when a handler returns. Changing a property,
such as progress.value, does not send anything by itself, even if the handler
then awaits something. To show progress part-way through, make the handler a
generator - each yield sends pending updates and gives the loop a chance to run:
def handle_click(e):
for i in range(10):
do_chunk(i)
progress.value = (i + 1) / 10
yield
The work still runs on the event loop, so keep each chunk short. For heavy chunks, offload them instead (see below).
In an async handler, call update() before an await instead. update() queues
the change, and the await gives the loop a turn to send it. A handler that calls
update() is not updated automatically when it returns, so call it again for the
final value:
async def handle_click(e):
status.value = "Loading..."
status.update() # sent while fetch_status() runs
status.value = await fetch_status()
status.update()
Showing a first screen at startup
main() follows the same rule. If it does slow work, such as loading data or
building a large UI, nothing reaches the screen until that work is done. To show
a loading screen first, yield before the slow part:
def main(page: ft.Page):
loading = ft.ProgressRing()
page.add(loading)
yield # sends the loading screen
data = load_data() # slow
page.controls.remove(loading)
page.add(build_ui(data))
The app does not respond to events while load_data() runs. In an async main(),
run the slow work in a thread instead (see Threading): awaiting it
sends the loading screen and keeps the app responsive:
async def main(page: ft.Page):
loading = ft.ProgressRing()
page.add(loading)
data = await asyncio.to_thread(load_data)
page.controls.remove(loading)
page.add(build_ui(data))
Background tasks
Use page.run_task() to start a coroutine that outlives the
handler that created it. For example, a self-updating countdown control:
import asyncio
import flet as ft
@ft.control
class Countdown(ft.Text):
seconds: int = 60
def did_mount(self):
self._task = self.page.run_task(self.update_timer)
def will_unmount(self):
self._task.cancel()
async def update_timer(self):
while self.seconds:
mins, secs = divmod(self.seconds, 60)
self.value = f"{mins:02d}:{secs:02d}"
self.update()
await asyncio.sleep(1)
self.seconds -= 1
def main(page: ft.Page):
page.add(Countdown(seconds=120), Countdown(seconds=60))
ft.run(main)
Pass the coroutine function, not a called coroutine - page.run_task(self.update_timer),
not page.run_task(self.update_timer()). It raises TypeError otherwise.
run_task() returns a future, so the control can cancel its own work in
will_unmount(), as above. did_mount() and will_unmount() are sync, which is
why background work is started with run_task() rather than awaited there.
In async code, you can also await the future to get the coroutine's return value,
or the exception it raised. For example, to start two tasks and wait for both:
async def load(e):
users = page.run_task(fetch_users)
orders = page.run_task(fetch_orders)
show(await users, await orders)
The future keeps the outcome after you await it, so keep a reference to it if you
also need to cancel the task or check it later. As with asyncio tasks,
cancelling the coroutine that awaits the future also cancels the task.
If you are already awaiting the future when the task fails, you can catch the
exception at the await without a duplicate log entry from run_task().
If nobody is awaiting the future when the task fails, Flet immediately logs
the traceback on the flet logger. Awaiting the future later still raises
the same exception.
Threading
Some libraries have no async version. To keep a blocking call off the loop, run it in a thread.
| Use | When |
|---|---|
await asyncio.to_thread(fn, *args) | you need the result back |
page.run_thread(fn, *args) | fire-and-forget |
page.loop.run_in_executor(pool, fn) | you want your own pool |
asyncio.to_thread() is the usual choice:
async def handle_click(e):
response = await asyncio.to_thread(requests.get, "https://api.example.com/items")
output.value = response.text
page.run_thread() runs the function with the current
page context and discards its return value. Flet catches exceptions from the
function and logs their tracebacks on the flet logger. This also applies
on static websites, where Pyodide runs the function inline.
def handle_click(e):
page.run_thread(write_log_file, "clicked")
For your own pool - to bound how many operations run at once, for example - use
page.loop with an executor you control:
pool = ThreadPoolExecutor(max_workers=2)
async def handle_click(e):
await page.loop.run_in_executor(pool, transcode, path)
Flet's own shared pool is available as page.executor.
Exceptions
Where an unhandled exception ends up depends on where it is raised:
| Raised in | What happens |
|---|---|
main() or an event handler | Flet logs it with its traceback and shows the error in the app. |
A page.run_task() coroutine | Awaiting the future raises the exception. If nobody is awaiting it when the task fails, Flet also logs the traceback on the flet logger. |
asyncio.to_thread() or page.loop.run_in_executor() | It is raised where the call is awaited, so a handler that awaits it reports it as above. |
A page.run_thread() function | Flet catches the exception and logs the traceback on the flet logger, including on Pyodide. |
A threading.Thread you start | It goes to threading.excepthook, which prints it to stderr. |
So catch exceptions inside a function you run in your own thread, and handle or log them there:
def poll_device():
try:
...
except Exception:
logging.exception("Polling the device failed")
Logged messages and stderr go to your terminal, or to the
console log of an app built with
flet build. An app packaged with
flet pack or PyInstaller as a windowed app -
the default for flet pack unless you pass --debug-console - has no console, so
log to a file to keep them:
logging.basicConfig(filename="app.log", level=logging.INFO)
CPU-bound work
Threads keep the UI responsive but do not make pure-Python work faster: the GIL lets only one thread run Python bytecode at a time. C extensions that release the GIL - NumPy, Pillow, most database drivers - do parallelize in plain threads.
For pure Python that needs more than one core:
| Runs on | Available on | |
|---|---|---|
| Subinterpreters | N interpreters, one process | desktop, mobile, dynamic web (Python 3.14+) |
| Multiprocessing | N processes | desktop only |
Subinterpreters are the option that also works on iOS and Android, where an app cannot spawn child processes. Multiprocessing gives full isolation and lets you hard-cancel a worker.
To report progress from either, update the UI as futures complete:
async def handle_click(e):
done = 0
with InterpreterPoolExecutor() as pool:
futures = [page.loop.run_in_executor(pool, do_chunk, i) for i in range(100)]
for future in asyncio.as_completed(futures):
await future
done += 1
progress.value = done / 100
progress.update()
Worker functions must be defined at module top level. See Subinterpreters for the rules and full examples.
Platform differences
| Threads | Subinterpreters | Multiprocessing | |
|---|---|---|---|
| Desktop | ✅ | ✅ (3.14+) | ✅ |
| iOS, Android | ✅ | ✅ (3.14+) | ❌ |
| Dynamic website | ✅ | ✅ (3.14+) | ✅ |
| Static website | ❌ | ❌ | ❌ |
A static website runs your app in the browser on
Pyodide, a
single-threaded WebAssembly runtime.
asyncio.to_thread() is not usable there and
page.run_thread() runs its callable inline, so it offers
no relief from blocking.
With no threads available, the only options are to chunk the work and yield
between chunks, use a real async library, or move the work to a server. A
dynamic website runs your Python
server-side as an ordinary CPython process, so everything above applies there as
it does on desktop.
Sync PubSub subscribers run on the thread pool rather than on the event loop, so a blocking subscriber will not freeze the UI. Event handlers are not treated this way.