Hello,
I’m facing an issue with managing shared data between callbacks in a Django Dash application.
During local development, I used a global variable initialized at server startup to store my data, allowing all callbacks to access it. However, in production, the data remains static as it reflects the state at server startup.
To address this, I followed the documentation: Dash - Sharing Data Between Callbacks.
Solution 1:
I moved the data computation to a specific callback, serialized my Pandas DataFrames into JSON, and stored them in a dcc.Store
. Each callback then reads this data and deserializes it back into a DataFrame. However, this process is slow (around 0.2 seconds per DataFrame), which significantly slows down the application.
Solution 2:
I implemented caching using flask_caching
, configured as follows:
CACHE_CONFIG = {
'CACHE_TYPE': 'redis',
'CACHE_REDIS_URL': REDIS_URL
}
My data-fetching function uses this cache:
@cache.memoize(timeout=60) # Cache timeout: 60 seconds
def fetch_data():
# Data computation
pass
However, when the serve_layout
is loaded for the first time, the 4 callbacks that call this function are executed simultaneously, resulting in the data being computed 4 times.
Do you have any recommendations to avoid these redundant computations and improve performance?
Thank you in advance!