Visual indicator to check if app is blocking main thread

If you’d like to see if any tasks are blocking, just add this to your app’s layout:


import asyncio
import panel as pn

pn.extension()

async def increment():
    await asyncio.sleep(0.05)
    progress.value = (progress.value + 1) % 101

progress = pn.indicators.Progress(value=0)
pn.state.add_periodic_callback(increment, period=100)
progress.servable()

For example:

import time
import asyncio
import panel as pn

pn.extension()

async def increment():
    await asyncio.sleep(0.05)
    progress.value = (progress.value + 1) % 101

async def blocking_task(event):
    time.sleep(3)
    with blocking_button.param.update(name="Blocking task complete"):
        await asyncio.sleep(3)

async def async_task(event):
    await asyncio.sleep(3)
    with async_button.param.update(name="Async task complete"):
        await asyncio.sleep(3)

progress = pn.indicators.Progress(value=0)
blocking_button = pn.widgets.Button(name="Run Blocking Task", on_click=blocking_task)
async_button = pn.widgets.Button(name="Run Async Task", on_click=async_task)
pn.state.add_periodic_callback(increment, period=100)
pn.Row(progress, blocking_button, async_button).servable()

When blocking button is clicked, the progress bar stops

1 Like

Very useful and easy to do. Add this to Best Practices? And add (link to) it to the documentation of async?

It’s probably not a best practice, but should be in some performance / async doc