Plotly dash with multiprocessing queue

I have a python script to get data from a sensor then save it into SQLite database then plot it with dash in Browser. That’s all must be in interval of 1 sec ± 0.002 sec. I can get it with this accuracy when I ignore the dash, but once I use dash and do anything in browser it will execute the script 2 times per second with unstable intervals between two points. I tried to deliver data between function and callback per multiprocessing queue to be faster and stabilizer, but it does not work also. Is there any suggestion for this problem?

the 1. function to sample data once per sec

def value_adc(n):
   conn = sqlite3.connect(...)
   curs = conn.cursor()
   curs.execute("INSERT INTO mess values(((julianday('now') - 2440587.5) * 86400.0), (?))", ([(ADC)]))
   df = pd.read_sql_query("SELECT * FROM mess ORDER BY timestamp DESC LIMIT 10", conn)
   row = df.iloc[:n,:]
   Timer(1, value_adc(), args=None, kwargs=None).start() 
and the callback function is

def update_Graph(n, on): if on == True:

raw = queue.get()
w =  numpy.asarray(row[df.columns[0]])
v = numpy.asarray(row[df.columns[1]])
data = go.Scatter( x = w , y = v, hoverinfo='text+name+y', name='Scatter', mode= 'lines+markers', )
layout = go.Layout(xaxis = dict(range=[min(w, default=0),max(w, default=0)]), yaxis = dict(range=[-1,6]))
figure = go.Figure({'data' :data, 'layout' : layout})
return figure

else: no_update 

Is there any way to sample and plot the data with at the same time accurate ?


Hello @alejandrosando,

What you could do, is first, see if using px.scatter will work quicker than trying to convert those columns to numpy arrays each time.

If that doesnt speed you up enough, you could pass the data separately and then use a clientside callback to update that end.

Something like this may help you:

thank u
I will try the px.scatter first then the second way.

1 Like