首页 > 解决方案 > 瓶子:BrokenPipeError:[Errno 32] 断管

问题描述

我编写了一个小 Python,它应该通过使用 LED-Strip 做一些事情来对一些 webhook 做出反应。我使用启动时启动的 Xterm 窗口在运行 Raspbian 的 Raspberry Pi 上运行它。该程序启动并运行了几分钟,但随后停止工作。我做了一些调试,发现有时程序会在一个 CPU 内核上使用 100%,有时它会吐出“BrokenPipeError: [Errno 32] Broken pipe”错误,有时它什么也不做。我试过单独运行发送到 LED 条,效果很好。我也尝试过使用不同的 Bottle 服务器,但也无济于事。

这是我发现的回溯:

*Private IP* - - [25/Mar/2020:17:21:31 +0200] "GET /setcolor/255/0/0 HTTP/1.1" 200 0 "-" "python-requests/2.23.0"
Traceback (most recent call last):
  File "/home/pi/.local/lib/python3.7/site-packages/bottle.py", line 868, in _handle
    return route.call(**args)
  File "/home/pi/.local/lib/python3.7/site-packages/bottle.py", line 1748, in wrapper
    rv = callback(*a, **ka)
  File "/home/pi/Documents/Python/LED-Webhooks.py", line 59, in index
    light.mode = magichue.NORMAL
  File "/home/pi/.local/lib/python3.7/site-packages/magichue/magichue.py", line 357, in mode
    self._set_mode(mode)
  File "/home/pi/.local/lib/python3.7/site-packages/magichue/magichue.py", line 369, in _set_mode
    receive=self.confirm_receive_on_send
  File "/home/pi/.local/lib/python3.7/site-packages/magichue/magichue.py", line 114, in _send_with_checksum
    self._send(data)
  File "/home/pi/.local/lib/python3.7/site-packages/magichue/magichue.py", line 105, in _send
    return self._sock.send(data)
BrokenPipeError: [Errno 32] Broken pipe

这是整个程序:

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():
    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


@route('/fadein')
def index():
    print("Im here")
    fadein(0, 0, 0)


@route('/setcolor/<r>/<g>/<b>')
def index(r, g, b):
    print("Im here")
    light.mode = magichue.NORMAL
    light.rgb = (int(r), int(g), int(b))


run(host='0.0.0.0', port=4783, server='paste')

标签: python-3.xraspberry-pibottle

解决方案


经过大量调试后解决,显然Magichue库忘记了我最初的声明:

light = magichue.Light('192.168.1.36')

一段时间后。


推荐阅读