Background Callbacks in Dash

Hi!

I’m using background callbacks for the first time. I’m wondering if this is expected behavior that others have seen or if this is a “me” problem.

I was able to follow the examples to create background callbacks from this site: Background Callbacks | Dash for Python Documentation | Plotly

Whenever I trigger a callback though it restarts my entire app. I’ve tested everything from my more complex app to a simple example that’s shown on the website. The examples online are less noticeable because reloading the app is quick enough, however with the more complex apps it adds even more lag to my callbacks that are already slow. Has anyone else encountered this issue and is there ideas to prevent my app from restarting?

Example code that I modified from the link above. In this example the print statement “App starting” is printed each time I trigger the callback when background callback is enabled. That is my indicator that the app is restarting. I also get the same response if I have debug mode set to True or False. I have also moved the cache folder outside my working directory. This hasn’t changed the behavior either. The only way I can get my app to not restart is to disable the background callback and only use standard callbacks.

Thanks in advance!

Scott

import time
import os
import diskcache
from dash import Dash, DiskcacheManager, Input, Output, html, callback

print("App starting")

# Diskcache for non-production apps when developing locally

cache = diskcache.Cache("./cache")
background_callback_manager = DiskcacheManager(cache)

app = Dash(__name__, background_callback_manager=background_callback_manager)

app.layout = html.Div(
    [
        html.Div(
            [
                html.P(id="paragraph_id", children=["Button not clicked"]),
                html.Progress(id="progress_bar", value="0"),
            ]
        ),
        html.Button(id="button_id", children="Run Job!"),
        html.Button(id="cancel_button_id", children="Cancel Running Job!"),
    ]
)

@callback(
    output=Output("paragraph_id", "children"),
    inputs=Input("button_id", "n_clicks"),
    background=True,
    running=[
        (Output("button_id", "disabled"), True, False),
        (Output("cancel_button_id", "disabled"), False, True),
        (
            Output("paragraph_id", "style"),
            {"visibility": "hidden"},
            {"visibility": "visible"},
        ),
        (
            Output("progress_bar", "style"),
            {"visibility": "visible"},
            {"visibility": "hidden"},
        ),
    ],
    cancel=Input("cancel_button_id", "n_clicks"),
    progress=[Output("progress_bar", "value"), Output("progress_bar", "max")],
    prevent_initial_call=True
)
def update_progress(set_progress, n_clicks):
    total = 5
    for i in range(total + 1):
        set_progress((str(i), str(total)))
        time.sleep(1)

    return f"Clicked {n_clicks} times"


if __name__ == "__main__":
    app.run(debug=True)