Callback works but the object is not updated properly

Hi,
there is a callback that works well, there is a print at the output that always shows the correct information in the console, but children where the result is returned is updated in the browser every other time or even less often, why can this happen?

@app.callback(
    Output(component_id='ar_all', component_property='children'),
    [Input(component_id='interval_realtime_metrics_ar', component_property='n_intervals')]
)
def realtime_metrics_update1(virt):
     ......
     print(x)
     return x
dbc.Container(
    [
        html.H1(children='AR'),
        html.Hr(className='my-2'),
        html.Div(id='ar_all'),
        html.Hr(className='my-2', style={'padding-bottom': '1%'}),
        dcc.Interval(
            id='interval_realtime_metrics_ar',
            interval=7 * 1000,  # in milliseconds
            n_intervals=0
        )
    ]
)

so i have interval=7 seconds, and the callback works as expected in time, but out of 100 responses, only 30 updates occur in the browser. can this be due to the fact that the application is used by 2 users?

I noticed that when the user is alone, everything is fine, each update reaches the browser, when the second one connects, it starts skipping updates, when the third one does not update children in the browser

What is x (i.e. a string, an object, …)? How long does the callback execution take (i.e. is it fast ~ ms, or slow ~ seconds)?

@Emil something about 5 seconds

What is x? is it a global variable or does the callback create it? we need more info about what’s gonig on in the callback. Also how are you running your app? are you using the development server or gunicorn? If it’s the latter how many workers?

In that case, my guess would be that the callback starts taking too long when multiple users are connected. If the callback execution time exceeds the interval, it will never return, and you’ll this see updates skipping.

@Anthony1223 x is a variable that is formed inside the callback, this is a query to the database. this query can actually take a few seconds…

server = flask.Flask(__name__)
app = dash.Dash(__name__, server=server)

app.layout = html.Div(
    [
        header,
        html.Div([
            html.Div(
                html.Div(id='page-content')
            ),
        ], className='container-width'),
        dcc.Location(id='url', refresh=False),

    ]
)

@app.callback(
    Output(component_id='ar_all', component_property='children'),
    [Input(component_id='interval_realtime_metrics_ar', component_property='n_intervals')]
)
def realtime_metrics_update1(virt):
    z = query_to_datavase
    x = dbc.Col(
        [
            dbc.Row(z),
            dbc.Row(z),
            dbc.Row(z)
        ],
        style={'min-width': '200px', 'font_size': '26px'}
    )
     return x

if __name__ == '__main__':

    twisted = Twisted(server)
    twisted.run(host='192.168.92.110', port=8050, debug=True)

@Emil how it is possible to organize a multiuser mode in that case? if I have a dashboard that just needs to update its metrics and display them the same for everyone…

If the issue is what I suggested, you could either increase the interval so that you are sure that it’s long enough, or alternatively try out the BlockingCallbackTransform from dash-extensions,

1 Like

In addition to Emil’s answer, might I suggest caching? Should handle multi-user mode better.

@Emil I tried, the problem persisted ((Perhaps I didn’t explain well, I write print before doing return from callback and it definitely prints everything to the console, as it should be in time, but return does not update the information on the object itself (in the browser …according to the console, my application works perfectly.) The data for return means already there, but it does not work as I expect.

@Emil
I came across such a thing - for each user the callback is executed separately and since the callback contains a query to the database, this happens simultaneously for each connected user. Can I update one Div at the same time for all users in one operation? maybe there is some global callback?)

in fact, I want to make one DIV in the application that will be updated for everyone every 5 seconds,
and now there is an update for each user who launched the application separately, the more users, the more copies of the callback are launched

thanks for your time!