I'm making a 7 segment display (4 displays) scoreboard with raspberry pi 3. I made a python program that reads 2 numbers from a database and stores them in a variable, checks how many digits are there (since i can only send one digit to one display) and then it sends the proper information to the GPIO pins. Each digit is on for 0.001s and so in 0.004s is goes through all 4. It then loops for a total of 200 repeats before going back and checking if any changes have been made in the database.
However, while it is rereading from the database there is a slight moment that all displays are off. I'm wondering if it's possible to continue the loop(the one with 200 repeats) with the previously stored variable and only restart it with the new ones after database finished reading the new information.
#i have set up the database and all other important stuff outside this loop
while 1:
digitTipe = 0
digitTipe2 = 0
timer = 0 #counter for the GPIO loop
#it gets the info from db and then it lists the digits
cur.execute("SELECT gol_domaci FROM tekme ORDER BY id DESC LIMIT 0, 1")
db.commit()
home_team = cur.fetchall()
for q in home_team-:
digits= list(int(d) for d in str(q[0]))
#same but for the other team
cur.execute("SELECT gol_gosti FROM tekme ORDER BY id DESC LIMIT 0, 1")
db.commit()
guest_team = cur.fetchall()
for e in guest_team:
digit2 = list(int(d) for d in str(e[0]))
#here checks if both digits are the same (11, 22, 33...), is just one digit(3, 6, ...) or if is just a random number (12, 23, 45,...)
#based on these results the GPIO output knows how to properly send out voltage... i tried with other methods but this one works for me
if len(digit) < 2:
digitTipe = 1
else:
if digit[0] == digit[1]:
digitTipe = 2
if len(digit2) < 2:
digitTipe2 = 1
else:
if digit2[0] == digit2[1]:
digitTipe2 == 2
while timer < 200: #this is the loop that has code for GPIO pins
#("insert digit output code")
The "lag" isn't to bad, only for a 0.1s at most, but it is noticeable and annoying so my "coworkers" want it fixed. But if possible i don't want to make two separate code files
i'm sorry for the bad quality of the coding. this is my first "real" program in python and in general. i'm also sorry if i'm not enough specific as it also happens to be my first question on stackoverflow. thanks in advance!
From what I gather, the while timer < 200: …
loop activates the display. So when this loop is not being executed because of cur.execute(…)
and so on, the display is deactivated.
The solution is to use asynchronous programming. Here is a good question on the topic: asynchronous programming in python
This is an example copied and pasted from one of the answers (author Jesse Dhillon):
from threading import Thread
def background_stuff():
while True:
print "I am doing some stuff"
t = Thread(target=background_stuff)
t.start()
# Continue doing some other stuff now
You can create two functions similar to background_stuff
, one to handle the display, another to fetch information from the database. Then instantiate two threads based on those two functions, and start both. So:
# declare variables here
def handle_display():
while True:
# put here what is under the `while timer < 200: … ` loop
def fetch_from_db():
# every time timer runs out, fetch info from database
# and store in variables declared at line 1
t1 = Thread(target=handle_display)
t2 = Thread(target=fetch_from_db)
t1.start()
t2.start()