# 1 sec refresh Interval for a 5 sec task

**URL:** <https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624>\
**Category:** Dash Python\
**Created:** [August 22, 2019, 9:04am UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624 "2019-08-22T09:04:01Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![noman](https://avatars.discourse-cdn.com/v4/letter/n/d6d6ee/32.png) [@noman](https://community.plotly.com/u/noman)\
**Post date:** [August 22, 2019, 9:04am UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624/1 "2019-08-22T09:04:02Z")

</div>

If there is a callback that is triggered every 1 second with the Interval component, but the callback function has an execution time of say 5 seconds, will dash app choke and get stuck? I want to understand what could be happening in the background in this case.

layout = html.Div(children = [  
html.P(id = ‘update\_p’),  
dcc.Interval(id = ‘refresh’, interval=1\*1000, n\_intervals=0),  
])

@app.callback(  
Output(‘update\_p’,‘children’),  
Input(‘refresh’,‘n\_intervals’)  
)  
def update(n\_intervals):  
time.sleep(5)  
return n\_intervals

Thanks

---

<div class="post-metadata">

**Author:** ![byronz](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/byronz/32/5727_2.png) [@byronz](https://community.plotly.com/u/byronz)\
**Post date:** [August 22, 2019, 2:28pm UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624/2 "2019-08-22T14:28:14Z")

</div>

working sample code

```python
import time
import dash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

app = dash.Dash( __name__ )

app.layout = html.Div(
    [
        html.P(id="update_p"),
        dcc.Interval(id="refresh", interval=1 * 1000, n_intervals=0),
    ]
)

@app.callback(Output("update_p", "children"), [Input("refresh", "n_intervals")])
def update(n_intervals):
    time.sleep(5)
    print("sleeped")
    return n_intervals

if __name__ == " __main__":
    app.run_server(debug=True)

```

@noman dash will handle that with the queue

 ![queue](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/8/83fcafa31cb1bda72c65e7466c8a6de937241036.gif)

---

<div class="post-metadata">

**Author:** ![noman](https://avatars.discourse-cdn.com/v4/letter/n/d6d6ee/32.png) [@noman](https://community.plotly.com/u/noman)\
**Post date:** [August 23, 2019, 12:25am UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624/3 "2019-08-23T00:25:08Z")

</div>

@byronz thanks so much for the input!

---

<div class="post-metadata">

**Author:** ![yg400](https://avatars.discourse-cdn.com/v4/letter/y/ecae2f/32.png) [@yg400](https://community.plotly.com/u/yg400)\
**Post date:** [August 25, 2019, 4:38pm UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624/4 "2019-08-25T16:38:43Z")

</div>

Hi, I am using your code to see how dash.Interval really works. I can see that the calls from Interval component are queued up like you showed in the gif, but the layout is not updated with n\_intervals number. However, if I reduce the sleep time to 0.5 seconds in which case the interval time (1s) is enough for the function to terminate, the html.P component can be updated by n\_interval:

 ![interval](https://us1.discourse-cdn.com/flex024/uploads/plot/original/2X/2/2448208d635c9781cc47a4ea1555168cc0d45b68.gif)

It seems to me that the output components will stop updating if the callback function is ‘cutoff’ by another call from Interval. I am wondering if we have a solution to this issue, or at least throw some exceptions telling me that it is terminated immaturely.

---

<div class="post-metadata">

**Author:** ![jblum](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jblum/32/12545_2.png) [@jblum](https://community.plotly.com/u/jblum)\
**Post date:** [September 14, 2020, 4:42pm UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624/5 "2020-09-14T16:42:09Z")

</div>

I’m having the same issue, where I’m triggering a callback once per second, but they seem to be piling up in the browser and eventually crashing the app. Two questions:

1. how can I effectively delay the start of the interval callback until the page that finished its initial load of the layout (most of the piling up happens at initial page load)
2. I ideally want the callback to fire once per second, (the callback usually runs in a few hundred milliseconds so this should be possible, but it sometimes takes longer). Can I make sure that the next instance doesn’t trigger until the previous one has finished somehow?

---

<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:** [September 14, 2020, 4:45pm UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624/6 "2020-09-14T16:45:59Z")

</div>

I guess the simplest approach would be to create an is-running flag serverside (in a file, a memory cache, anything). You then set it to true before starting the task and false afterwards. And before starting the task, you check if it’s already running; if it is, raise a `PreventUpdate` exception.

---

<div class="post-metadata">

**Author:** ![jblum](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/jblum/32/12545_2.png) [@jblum](https://community.plotly.com/u/jblum)\
**Post date:** [September 14, 2020, 5:06pm UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624/7 "2020-09-14T17:06:35Z")

</div>

That general approach sounds reasonable, but wouldn’t I want that state flag to exist on the client side, not the server side? The pile-up seems to be happening on the client. If so, does dash have some easy built-in mechanism for writing/reading a session variable that would enable this?

---

<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:** [September 14, 2020, 5:21pm UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624/8 "2020-09-14T17:21:12Z")

</div>

If your callback is clientside, the flag should reside clientside. If your callback is serverside (i.e. normal callbacks written in python), the flag should be serverside (unless you make a callback chain with a client-side flag; that’s also an option). There have been some changes to the callback logic in Dash though, so i am not sure if the “first” callback will ever return, if subsequent callbacks are dispatched. If it doesn’t, your’ll need to store the result somewhere (e.g. in a file of memory cache) and read it in the subsequent callback.

---

<div class="post-metadata">

**Author:** ![johndy](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/johndy/32/14577_2.png) [@johndy](https://community.plotly.com/u/johndy)\
**Post date:** [March 17, 2021, 6:01pm UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624/9 "2021-03-17T18:01:23Z")

</div>

Hi @Emil, I have a similar situation where I have a 4 second interval triggering a callback that generally takes 2 seconds but sometimes because of network latency the requests get piled up. I understand the simplest approach is to “create an is-running flag serverside”. I’ve been trying to think/implement a way of doing this through call backs alone with the is\_running\_flag held in a dcc.store object, but because callbacks are synchronous, I think I would need to implement an asynchronous semaphore type solution like the following:

> <https://github.com/plotly/dash/issues/57>
>
> How can I handle asynchronous logic?
> For instance, how can I create a button 'sleep' which writes to a div 'sleeping' for...

Before I start implementing this, I wanted to confirm that’s the type of asynchronous solution you meant or if there is another simpler way?

---

<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:** [March 17, 2021, 6:15pm UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624/10 "2021-03-17T18:15:05Z")

</div>

I think it would be much simpler with a server side flag. Alternatively you could check out this thread,

> [@Preventing Wasteful Parallel Callbacks When Running Multiple Processes and Caching](https://community.plotly.com/t/preventing-wasteful-parallel-callbacks-when-running-multiple-processes-and-caching/18956/17):
>
> Looks like the issue is the default start method on MacOS changed from fork to spawn in Python 3.8 [https://docs.python.org/3/whatsnew/3.8.html#multiprocessing](https://docs.python.org/3/whatsnew/3.8.html#multiprocessing) Setting the start method back to fork is a quick work around e.g. from jitcache import Cache import time import multiprocessing as mp mp.set\_start\_method('fork') cache = Cache() @cache.memoize def slow\_fn(input\_1, input\_2, input\_3=10): print("Slow Function Called") time.sleep(1) return input\_1 \* input\_2 \* input\_3 print(s…

---

<div class="post-metadata">

**Author:** ![johndy](https://sea2.discourse-cdn.com/flex024/user_avatar/community.plotly.com/johndy/32/14577_2.png) [@johndy](https://community.plotly.com/u/johndy)\
**Post date:** [March 17, 2021, 11:36pm UTC](https://community.plotly.com/t/1-sec-refresh-interval-for-a-5-sec-task/27624/11 "2021-03-17T23:36:53Z")

</div>

Agree, it would be simpler, but how can that be done? Currently, I have a 4 second interval that is an input to trigger the callback, but if intervals pile up then they will also trigger the callback even if the prior interval call back is still running. I can add an is\_running\_flag in the callback output and at the end of the callback this is\_running\_flag can be set to False to signal the process is complete, but what is the mechanism at the start or between the Interval firing to the Callback being triggered that can set the is\_running\_flag to True, so if there is a pile up and the prior interval process is still running when the next interval triggers the callback, then a prevent update exception can be raised?
