【发布时间】:2020-03-31 22:49:41
【问题描述】:
我遇到了一个在 Raspberry Pi 上运行的 Python 程序的奇怪问题。编程应该接收一个 webhook,然后执行一些应该控制廉价 LED-Strip 的代码。 该程序可以正常启动并正常执行,但是如果我让它运行一段时间然后触发 webhook,程序会中断并开始在单个 CPU 内核上使用 100%。这发生在我的 PyCharm 电脑和 Raspberry Pi 上。真正让我失望的是,'prints()' 确实被执行了,但其余的都没有。
import time
import datetime
import magichue
from bottle import route, run
light = magichue.Light('192.168.1.36')
def flash(r, g, b):
light.mode = magichue.NORMAL
light.rgb = (0, 0, 0)
time.sleep(0.5)
light.rgb = (r, g, b)
time.sleep(0.4)
light.rgb = (0, 0, 0)
time.sleep(0.5)
light.rgb = (r, g, b)
time.sleep(0.4)
def fadein(r, g, b, tr=1):
light.mode = magichue.NORMAL
while tr <= 25:
r = r + 10
g = g + 10
b = b + 10
light.rgb = (r, g, b)
time.sleep(0.5)
tr = tr + 1
else:
light.rgb = (255, 255, 255)
@route('/flashgreen')
def index():
try:
print("Im here")
x = datetime.datetime.now()
if 10 <= x.hour <= 22:
time.sleep(0.1)
light.update_status()
if light.on:
pr = light.rgb
flash(0, 255, 0)
light.rgb = pr
else:
flash(0, 255, 0)
light.on = False
except:
pass
@route('/fadein')
def index():
try:
print("Im here")
fadein(0, 0, 0)
except:
pass
@route('/setcolor/<r>/<g>/<b>')
def index(r, g, b):
try:
print("Im here")
light.mode = magichue.NORMAL
light.rgb = (int(r), int(g), int(b))
except:
pass
run(host='0.0.0.0', port=4783)
【问题讨论】:
标签: python-3.x raspberry-pi bottle