# Duplicated outputs error when using same event as cancel trigger in multiple background callbacks

**URL:** <https://community.plotly.com/t/duplicated-outputs-error-when-using-same-event-as-cancel-trigger-in-multiple-background-callbacks/75620>\
**Category:** Dash Python\
**Created:** [May 29, 2023, 2:49pm UTC](https://community.plotly.com/t/duplicated-outputs-error-when-using-same-event-as-cancel-trigger-in-multiple-background-callbacks/75620 "2023-05-29T14:49:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![petro](https://avatars.discourse-cdn.com/v4/letter/p/50afbb/32.png) [@petro](https://community.plotly.com/u/petro)\
**Post date:** [May 29, 2023, 2:49pm UTC](https://community.plotly.com/t/duplicated-outputs-error-when-using-same-event-as-cancel-trigger-in-multiple-background-callbacks/75620/1 "2023-05-29T14:49:45Z")

</div>

Hi,

I’ve multiple celery background callbacks running in parallel. I’d like to cancel all of them by single button  
`cancel=Input("cancel_processing_button_id", "n_clicks"),`  
however this leads to (surprisingly) duplicated `Outputs` error:

```auto
In the callback for output(s):
  cancel_processing_button_id.id
Output 0 (cancel_processing_button_id.id) is already in use.
To resolve this, set `allow_duplicate=True` on
duplicate outputs, or combine the outputs into
one callback function, distinguishing the trigger
by using `dash.callback_context` if necessary.

```

Any ideas what is wrong would be appreciated.  
Thanks!

---

<div class="post-metadata">

**Author:** ![AIMPED](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/aimped/32/21758_2.png) [@AIMPED](https://community.plotly.com/u/AIMPED)\
**Post date:** [May 30, 2023, 11:00am UTC](https://community.plotly.com/t/duplicated-outputs-error-when-using-same-event-as-cancel-trigger-in-multiple-background-callbacks/75620/2 "2023-05-30T11:00:14Z")

</div>

> [@petro](#):
>
> `cancel_processing_button_id.id`

Hi. Do you use this as `Output()` for a callback?

---

<div class="post-metadata">

**Author:** ![petro](https://avatars.discourse-cdn.com/v4/letter/p/50afbb/32.png) [@petro](https://community.plotly.com/u/petro)\
**Post date:** [May 31, 2023, 7:08am UTC](https://community.plotly.com/t/duplicated-outputs-error-when-using-same-event-as-cancel-trigger-in-multiple-background-callbacks/75620/3 "2023-05-31T07:08:20Z")

</div>

Hi,  
No, there is no explicit reference to `cancel_processing_button_id.id` in the code.  
I only define button:

```auto
            dbc.Button(
                "Cancel processing",
                id="cancel_processing_button_id",
                className="ms-auto",
                n_clicks=0,
            )

```

and in code I use n\_click property of the button:

```auto
    @dash.callback(Output({'role': 'simulation_results', 'task': 'wave_functions'}, 'data'),
                   [Input({'role':'context', 'store': 'trigger_celery'}, 'data')],
                   [State({'role':'context', 'store': 'nanostructure'}, 'data'),
                    State({'role':'context', 'store': 'material_params'}, 'data')],
                   background=True,
                   cancel=Input("cancel_processing_button_id", "n_clicks"),
                   prevent_initial_call=True)
    def _simulate_wave_functions(_, nanostructure, materials_params):

```

(Similar code is repeated 3 times)

---

<div class="post-metadata">

**Author:** ![petro](https://avatars.discourse-cdn.com/v4/letter/p/50afbb/32.png) [@petro](https://community.plotly.com/u/petro)\
**Post date:** [May 31, 2023, 2:46pm UTC](https://community.plotly.com/t/duplicated-outputs-error-when-using-same-event-as-cancel-trigger-in-multiple-background-callbacks/75620/4 "2023-05-31T14:46:35Z")

</div>

@AIMPED I’ve dug into dash internals and found the following line:

> <https://github.com/plotly/dash/blob/ca3cca3ee3fd7d7a4b6eb1dfb6ba9e7c3027fa7a/dash/_callback.py#LL174C11-L174C11>

So, I guess it’s a “feature”.

---

<div class="post-metadata">

**Author:** ![Emil](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/emil/32/31734_2.png) [@Emil](https://community.plotly.com/u/Emil)\
**Post date:** [May 31, 2023, 5:36pm UTC](https://community.plotly.com/t/duplicated-outputs-error-when-using-same-event-as-cancel-trigger-in-multiple-background-callbacks/75620/5 "2023-05-31T17:36:09Z")

</div>

I guess it is because Dash internally creates a callbacks, which has this component as `Output`. Hence, for each similar repeat, a new such callback is created, thus leading to multiple callbacks that targets the same `Output`. Ideally for your use case, these callbacks should be changed in the source to include `allow_duplicate` flags. Until that happens, you should be able to monkey patch your way out by placing code like,

```auto
import dash

class PatchOutput(dash.Output):
    def __init__ (self, *args, allow_duplicate=True, **kwargs):
        super(). __init__ (*args, allow_duplicate=allow_duplicate, **kwargs)

dash.Output = PatchOutput
original = dash.callback
dash.callback = lambda *args, **kwargs: original(*args, prevent_initial_call=True,**kwargs)
dash._callback.callback = lambda *args, **kwargs: original(*args, prevent_initial_call=True,**kwargs)

```

at the top of your app file.

---

<div class="post-metadata">

**Author:** ![petro](https://avatars.discourse-cdn.com/v4/letter/p/50afbb/32.png) [@petro](https://community.plotly.com/u/petro)\
**Post date:** [June 6, 2023, 1:01pm UTC](https://community.plotly.com/t/duplicated-outputs-error-when-using-same-event-as-cancel-trigger-in-multiple-background-callbacks/75620/6 "2023-06-06T13:01:45Z")

</div>

Thanks @Emil for your hint. 🙂  
I’ve decided to go with separate cancel button per background callback for now. Though it would be nice to have that implemented properly one day.

One more observation: `cancel` /`running` actions of background callbacks seem not to work with compound ids (dicts) e.g.  
`id={'role': 'cancel_processing', 'task': task}` however they work fine when using plain strings `id=f'cancel_processing_{task}'`
