How to fix sluggish background callbacks

I’m attempting to implement a background callback process using the diskcache option. Using the sample code at Background Callbacks | Dash for Python Documentation | Plotly works just fine; however, the Example #1 code runs extremely sluggishly in my own app, sometimes taking 30+ seconds to complete (instead of the 2 second sleep in the example).

Working backwards to solve, I progressively commented out all of my code, piece by piece. There was no radical “aha!” when it was solved, but the response time slowly improved until it finally ran as normal. Of course my app was fully non-functional at that point.

Why should commenting out code cause the diskcache to run better? It seems like the background manager triggers a full background reload of everything (imports, etc.) whenever the callback is triggered?

Anyone experience the same behavior?

I’ve got the same issue - ever figure this out?

Hello @titanup,

There are a couple things at work.

There is the background polling which happens on 1 second intervals. The server is receiving the request, looking at the background for updates. So, this can lead to a delay in the response time just for this fact.

A couple of things to note, the server will respond to requests in a first come first served manner. These are handled in a synchronous manner. If you have other requests happening to the server then the polling will be in a line. The server responds to things from assets, components, callbacks, hot reload requests, etc. All of these can get in the way of a response coming for callbacks.

When hosting and being able to use more workers, there are more things to handle the requests and you are able to get through the queue faster. Other things you can do to help when hosted is to cache responses from the server.

1 Like