Why do long callbacks take a longer amount of time for a normal callback

I am having two issues with the @app.long_callback feature.

  1. Locally everything is working I make use of the diskcache long call back manager and my long callbacks and normal callbacks work completely fine. However a normal callback that only originally took a couple of seconds to get the response now can take up to two minutes when I have replaced @app.callback for @app.long_callback, the app seems to keep calling the URL many times, eventually the data comes but not as fast as before.

  2. Once I uploaded to the server my celery worker is ok/redis setup ok everything is ok, however when I click a button to get my results I get thrown a 502 error like so:

Callback error updating ..accordion-summary.children...analyze-output.data...analyze-output-download.data...graph-components.data..._long_callback_interval_1.disabled...analysis-button.disabled...cancel-button.disabled..._long_callback_store_1.data.

But if I change it back to a @app.callback and the redeploy it works completely fine. Once deployed with @app.long_callback I also still see the issue of it taking a long time for callbacks that should only take a few seconds (I thought redis would have sorted the issue out). There seems to be some sort of issue with @app.long_callback.

My code is set up like so:

app.py imports results.py and other content


#  Callback Manager
if os.getenv("IS_SERVER"):
    #  Create celery object
    celery_app = Celery(
        __name__,
        backend=BE_URL,
        broker=BR_URL,
    )
    #  Cache expires weekly - can increase or decrease this amount.
    long_callback_manager = CeleryLongCallbackManager(celery_app, cache_by=[lambda: launch_uid], expire=604800)
else:
    #  For use locally in Windows environment
    disk_cache = diskcache.Cache("./cache")
    long_callback_manager = DiskcacheLongCallbackManager(disk_cache, cache_by=[lambda: launch_uid], expire=604800)
    celery_app = None


inner_layout = html.Div(
    [
        html.Div(id="hidden-div", style={"display": "none"}),
        html.H1("Define path", className="text-center my-5"),
        select_nodes,
        results,
        footer,
    ],
    style={"padding": 20},
)
app.layout = html.Div(children=[header, inner_layout])

app.validation_layout = html.Div([
    header,
    inner_layout
])
app.run_server(debug=False)

results.py

results = html.Div(
    [
        dbc.Row(
            [
                html.Div(
                    [
                        dcc.Loading(
                            [
                                dbc.Button(
                                    "Analyze",
                                    color="primary",
                                    id="analysis-button",
                                    style={"backgroundColor": colors["green1"], 'margin-right': "100px"},
                                ),
                                dbc.Button(
                                    "Cancel",
                                    color="primary",
                                    id="cancel-button",
                                    style={"backgroundColor": colors["red1"]},
                                ),
                            ], color=colors["green2"]
                        )
                    ]),
                dcc.Store(id="analyze-output", storage_type="session"),
                dcc.Store(id="analyze-output-download", storage_type="session"),
                dcc.Store(id="graph-components", storage_type="session"),
            ],
            class_name="text-center",
        ),
        html.Hr(),
        dbc.Fade(
            [
                accordion,
            ],
            appear=False,
            id="div-body",
            is_in=False,
        ),
    ]
)
@app.long_callback(
    Output("accordion-summary", "children"),
    Output("analyze-output", "data"),
    Output("analyze-output-download", "data"),
    Output("graph-components", "data"),
    Input("analysis-button", "n_clicks"),
    State("select-PBG-start", "value"),
    State("select-BP-start", "value"),
    State("select-COCD-start", "value"),
    State("select-PBG-end", "value"),
    State("select-BP-end", "value"),
    State("select-COCD-end", "value"),
    running=[
        (Output("analysis-button", "disabled"), True, False),
        (Output("cancel-button", "disabled"), False, True),
    ],
    cancel=[Input("cancel-button", "n_clicks")],
    prevent_initial_call=True,
)
def update_output_data(
    n_clicks: bool,
    pbg_start: str,
    bp_start: str,
    cocd_start: str,
    pbg_end: str,
    bp_end: str,
    cocd_end: str,
):

 # my code to query db here

Hello mp252 welcome to the community.

Maybe help if you change Long Callbacks for Flask-Caching

https://dash.plotly.com/sharing-data-between-callbacks
https://dash.plotly.com/performance

1 Like

@SamuelVT Hi thanks for the welcome :grinning:

I did look into Flask-Caching, however the only problem with this is, the initial callback. So I have one callback that can take between 2-4 minutes the first time it is run if it is not cached therefore meaning the nginx will have a timeout. Do you think this is can be solved with Flask-Caching as well?

Hi mp252

Maybe if you use: Flask-Cache Dynamic Timeout