运行 python 循环在从数据库读取时不间断
running a python loop uninterrupted while it's reading from a database
我正在用 raspberry pi 制作一个 7 段显示器(4 个显示器)记分牌 3. 我制作了一个 python 程序,它从数据库中读取 2 个数字并将它们存储在一个变量中,检查有多少位数字(因为我只能将一位数字发送到一个显示器),然后它将正确的信息发送到 GPIO 引脚。每个数字持续 0.001 秒,因此在 0.004 秒内通过所有 4 个。然后它循环总共重复 200 次,然后返回并检查数据库中是否进行了任何更改。
但是,当它从数据库中重新读取时,有一小会儿所有显示都关闭了。我想知道是否可以使用先前存储的变量继续循环(重复 200 次),并且仅在数据库读取完新信息后才使用新变量重新启动它。
#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")
"lag" 还不错,最多只有 0.1 秒,但它很明显且令人讨厌,所以我的 "coworkers" 希望修复它。
但如果可能的话,我不想制作两个单独的代码文件
对于编码质量不佳,我深表歉意。这是我在 python 和一般情况下的第一个 "real" 程序。如果我不够具体,我也很抱歉,因为它也恰好是我关于 Whosebug 的第一个问题。
提前致谢!
据我所知,while timer < 200: …
循环激活了显示。因此,当由于 cur.execute(…)
等原因未执行此循环时,显示将被停用。
解决方案是使用异步编程。这是关于该主题的一个很好的问题:asynchronous programming in python
这是从其中一个答案(作者 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
您可以创建两个类似于 background_stuff
的函数,一个处理显示,另一个从数据库中获取信息。然后根据这两个函数实例化两个线程,并启动它们。所以:
# 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()
我正在用 raspberry pi 制作一个 7 段显示器(4 个显示器)记分牌 3. 我制作了一个 python 程序,它从数据库中读取 2 个数字并将它们存储在一个变量中,检查有多少位数字(因为我只能将一位数字发送到一个显示器),然后它将正确的信息发送到 GPIO 引脚。每个数字持续 0.001 秒,因此在 0.004 秒内通过所有 4 个。然后它循环总共重复 200 次,然后返回并检查数据库中是否进行了任何更改。
但是,当它从数据库中重新读取时,有一小会儿所有显示都关闭了。我想知道是否可以使用先前存储的变量继续循环(重复 200 次),并且仅在数据库读取完新信息后才使用新变量重新启动它。
#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")
"lag" 还不错,最多只有 0.1 秒,但它很明显且令人讨厌,所以我的 "coworkers" 希望修复它。 但如果可能的话,我不想制作两个单独的代码文件
对于编码质量不佳,我深表歉意。这是我在 python 和一般情况下的第一个 "real" 程序。如果我不够具体,我也很抱歉,因为它也恰好是我关于 Whosebug 的第一个问题。 提前致谢!
据我所知,while timer < 200: …
循环激活了显示。因此,当由于 cur.execute(…)
等原因未执行此循环时,显示将被停用。
解决方案是使用异步编程。这是关于该主题的一个很好的问题:asynchronous programming in python
这是从其中一个答案(作者 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
您可以创建两个类似于 background_stuff
的函数,一个处理显示,另一个从数据库中获取信息。然后根据这两个函数实例化两个线程,并启动它们。所以:
# 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()