python - 在从数据库读取时不间断地运行 python 循环
问题描述
我正在用树莓派 3 制作一个 7 段显示(4 个显示)记分牌。我制作了一个 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")
“滞后”还不错,最多只有 0.1 秒,但它很明显而且很烦人,所以我的“同事”希望它修复。但如果可能的话,我不想制作两个单独的代码文件
我很抱歉编码质量不好。这是我在 python 和一般情况下的第一个“真实”程序。如果我不够具体,我也很抱歉,因为它也恰好是我在 stackoverflow 上的第一个问题。提前致谢!
解决方案
推荐阅读
- amazon-web-services - 我需要使用 MemeCache 进行身份验证吗?
- python - 在运行时更改 django 模型的表名
- c - 指向类型定义结构的指针
- reactjs - 酶单元测试返回
在组件上运行测试时 - c# - 使用从 swagger 发送的流畅验证请求验证嵌套属性
- bash - 仅当文件与模式不匹配时,如何重命名文件?
- python - 两个未链接的列表,在一个中查找项目的位置并从另一个打印位置
- html - fab fa-whatsapp 出现在移动设备中,但我无法点击它
- codeigniter - 使用 curl 和 Codeigniter 3 获取标题字段
- python - Google Document Ai 为同一文件提供不同的输出