Plotly Dash on Respberry pi 3+B

I have a question. I have written a python script in a raspberry pi3 to get data from a sensor and plot it with dash in Browser once per second. It works very well with pretty good accuracy when I do not do anything in browser, whoever when I open a new tab or whatever the dash starts to get two values or more per second or starts to delay for more than a second!
has any one idea why or how to fix it?
→ the code

app = Dash(__name__)

app.layout = html.Div([ dcc.Graph(id='live', animate=True),dcc.Interval(id='update', interval = 1000,  max_intervals = -1),])

@app.callback( Output('live', 'figure'),
               Input('update', 'n_intervals'),
               Input('switch', 'on'),
def update_Graph(n , on):	
	if on == True:
		x2 = monotonic() - x1
		Timer(1 - x2, sampledata).start() #sample data from sensor and import into db
		####### figuir function
		figure = go.Figure({'data' :data, 'layout' : layout})
		return figure
		return no_update
app.run_server(host = '', port = , debug=True, use_reloader=True)

Hey @alejandrosando,

I was wondering if you got it working or not.

Have you tried turning off debug and reloaded?

Also, question, what is the Pi running?

Is it running pi-hole?

I actually need bedug to catch any mistakes in Dash code. And I have tried to turn off the use_reloder, but it does not make any difference.
I have Rasbian/raspberry pi os on the Pi

What other tasks is the Pi doing, other than running the server?

Nothing, just running this script. I even use another pc to open the browser with the IP

What IP is it running on?

Is there anything that would route requests from the router to hit it more often.

Also, have you tried spreading the interval out, 1 second seems rather quick. Maybe try 5 seconds?

I am using LAN with both pc and pi
I have not tried this yet, the main goal of the project is to get data point once per second.

On the pi, can you see how many requests are coming in?

Is it getting duplicated?

I have just tried to get the interval 2 seconds, and it was pretty accurate even while surfing.
Sorry, How can I found how many requests are coming in pi?

You’d have to watch the python console on the pi.

Glad 2 seconds is working.

Here’s a possible theory:

Your initial timeframe, every 1 second.

Here is what is happening on the network and server side of things:

  • it takes .2 seconds for the info to reach your server
  • your server takes .1 seconds to process the data
  • it takes .2 seconds to jsonify
  • it takes .2 seconds to transmit back
  • it takes .2 seconds to receive and update the client

So, every 1 second, you are using the network and server for .9 seconds. Which leaves .1 second for wiggle room.

If any part of the process takes slightly longer than expected, you start to run into a backlog of requests, because your requests are at a steady pace of 1 second.

Slowing it down to 2 seconds allows for the wiggle room.

Things that can effect your processing:

  • server heat (server doesnt get a chance to breath between requests)
  • network latency increases

This is why people typically will throttle API calls on servers. :smiley:

1 Like