Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1251

Python • RRDtool & python3 code optimization

$
0
0
Hello. The situation is as follows...

I use rrdtool to plot the outside and inside temperature graphs, the data of which is kept in a mysql database.

rrdtool update file is this:

Code:

#!/bin/bashcd /home/w1/year/rrdtool=/usr/bin/rrdtooldata1=$(python3 /home/w1/year/outtemp.py)data2=$(python3 /home/w1/year/hometemp.py)$rrdtool update year.rrd --template out:home N:$data1:$data2
outtemp.py file is this:

Code:

#!/usr/bin/python3import mysql.connectormydb = mysql.connector.connect(  host="192.168.0.101",  database="********",  user="w1",  passwd="********")mycursor = mydb.cursor()sql = "select temp from outsidetemp where id = (select max(id)-1 from outsidetemp)"mycursor.execute(sql)data = mycursor.fetchone()last = (data[0])mydb.commit()mycursor.close()mydb.close()print(last)
hometemp.py file is this:

Code:

#!/usr/bin/python3import mysql.connectormydb = mysql.connector.connect(  host="192.168.0.101",  database="********",  user="w1",  passwd="********")mycursor = mydb.cursor()sql = "select hometemp from outsidetemp where id = (select max(id)-1 from outsidetemp)"mycursor.execute(sql)data = mycursor.fetchone()last = (data[0])mydb.commit()mycursor.close()mydb.close()print(last)
My question is how can I optimize the speed and the code to use with one python file (one connection to mysql - not two) for the two values (data1 & data2) and to use them with rddtool update file?
I`ll need help for python code and bash code as well :roll: Thanks

Statistics: Posted by independent — Sat Aug 10, 2024 9:17 am



Viewing all articles
Browse latest Browse all 1251

Trending Articles