Python dash - thread safe

Hi,
in my app i have very big dcc.store that stored a global data for the app
and meny callback are using and update this store data
and sometimes its seemed that some of the callback work in parllel and then the dcc store get None value somehow.
and its cause me many problem.
do you have any solution?
maybe a way to prevent a callback to run while another callback doesnt finish?
thank you!!

thank you
if i am using lock,
and i change the lock value inside the callback, it is changed also without return it in output?

Do you have an answer for me?

I’m afraid the main purpose of the answer was adding a SPAM link.

I regret my approach is always to design apps to ensure this kind of parallelism (two callbacks running in parallel updating the same store) can’t happen.

And I’ve come to believe, rightly or wrongly, that it has to be done this way. dcc.Store() doesn’t have the kind of support for parallel updates as, say a relational database does.

I don’t know if the locking approach suggested above can be made to work reliably, but my gut feel says probably not. If anyone does use it successfully though it would be interesting to hear from them.

so what do you suggest to use instead of dcc.store?

I’m not suggesting you don’t use dcc.Store(), but maybe not one single large dcc.Store holding all global data?

You may be able to avoid parallel updates to a store by some combination of

  • splitting the one Store() into several
  • tracking what sequence callbacks are triggered in, and changing what triggers them
  • combining multiple callbacks that currently run in parallel into a single callback

I should add I’m not anything like 100% sure this is the right approach for you, without being able to see more detail of what you’re doing. And if the app is big and complex it could also be a lot of work.

I have a lot of data in the dcc store and it can be a lot of work as you said
so maybe do you have any solution to prevent it without changing current design?

In as much as the cause of the parallelism is two callbacks having the same trigger, it’s always possible to make them run sequentially by changing, say:

@app.callback(
    ..., #outputs
    Input("button1","n_clicks"),
    ... # other inputs
)
def callback1(nclicks, ...):
    ...

@app.callback(
    ..., # outputs
    Input("button1","n_clicks"),
    ... # other inputs
)
def callback2(nclicks, ...):
    ...

by adding a dcc.Store(id=‘store-trigger1’) and doing something like:

@app.callback(
    Output('store-trigger1', 'data'),
    ..., # other outputs
    Input("button1","n_clicks"),
    ... # other inputs
)
def callback1(nclicks, ...):
    ...
    return nclicks, ...

@app.callback(
    ..., #outputs
    Input('store-trigger1','data',
    State("button1","n_clicks"),
    ... # other inputs
)
def callback2(_, nclicks,...):
    ...

That might be a relatively low-effort approach to cutting down parallelism, but with the same warning as before, I’m nothing like 100% sure this actually will solve your problems.