脚本在Raspberry pi上作为服务运行时占用了太多cpu资源

2024-09-30 23:38:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这个代码片段在树莓皮上运行。它基本上是拍人们进出我房间的照片。你知道吗

import RPi.GPIO as GP
import os
import socket


def opencallback(channel):
    print(GP.input(channel))
    if GP.input(channel):

        global closeEvent
        closeEvent = 1
    else:
        global openEvent
        openEvent = 1


def transmit(message):
    s = socket.create_connection((host, port))
    s.send(message)
    s.close()


def capture(cam, gpiolist, quick):
    GP.output(cam1, gpiolist[0])
    GP.output(cam2, gpiolist[1])
    GP.output(cam3, gpiolist[2])

    if quick:
        cmd = "raspistill -o capture_%d.jpg -t 2" % cam
    else:
        cmd = "raspistill -o capture_%d.jpg" % cam
    os.system(cmd)


# init
GP.setwarnings(False)
GP.setmode(GP.BOARD)

cam1 = 7
cam2 = 11
cam3 = 12
doorIn = 40
ledOut = 38

GP.setup(cam1, GP.OUT)  # camera Mux1
GP.setup(cam2, GP.OUT)  # camera Mux2
GP.setup(cam3, GP.OUT)  # camera Mux3
GP.setup(ledOut, GP.OUT)  # LED OUT GPIO 20
GP.setup(doorIn, GP.IN)  # Door detector in GPIO 21

GP.add_event_detect(doorIn, GP.BOTH, callback=opencallback)

GP.output(ledOut, False)
openEvent = 0
closeEvent = 0
host = '192.168.1.111'
port = 13579

# main
while True:
    if openEvent == 1:
        transmit("2-01")
        capture(2, (False, True, False), True)  # front cam
        transmit("2-03")
        openEvent = 0
    else:
        pass

    if closeEvent == 1:
        transmit("2-02")
        GP.output(ledOut, True)
        capture(3, (False, False, True), False)
        GP.output(ledOut, False)
        transmit("2-04")
        closeEvent = 0
    else:
        pass

通常,我只是通过命令行使用标准调用来运行它,它不会加载系统。你知道吗

但是,我最近使用systemd/systemctl将其转换为一个服务,因为我想在启动pi时在后台加载该脚本。现在,这个脚本自己吞噬了整个处理器内核(正如htop所报告的那样)。在转换过程中,我没有对代码本身做任何更改,当我以旧方式运行它时,它仍然可以正常工作。大多数情况下,它只是简单地运行while循环,什么也不做,等待GPIO的回调,然后执行一些函数并返回while传递行为。你知道吗

我的问题是:是什么导致了这两种执行方法在计算功耗上的差异?有办法解决这个问题吗?你知道吗


Tags: falsetrueoutputgpioifsetupoutelse
2条回答

边缘检测/回调代码非常有效,仅在事件发生时调用。但是,顶层while True:循环的效率非常低。你知道吗

考虑以下修改:

try:
    import Queue as queue # Python 2.x
except ImportError:
    import queue          # Python 3.x

eventQueue = queue.Queue()

def opencallback(channel):
    eventQueue.put(GP.input(channel))

# ...your existing setup code here, specifically including:
GP.add_event_detect(doorIn, GP.BOTH, callback=opencallback)

while True:
    event = eventQueue.get()
    if event:
        transmit("2-01")
        capture(2, (False, True, False), True)  # front cam
        transmit("2-03")
    else:
        transmit("2-02")
        GP.output(ledOut, True)
        capture(3, (False, False, True), False)
        GP.output(ledOut, False)
        transmit("2-04")

注意这与systemd无关任何其他调用方法也会有相同的CPU使用问题。你知道吗

为什么不让回调直接触发打开和关闭事件的操作?只是让环路短一点时间。睡眠你知道吗

def opencallback(channel):
    print(GP.input(channel))
    if GP.input(channel):
        transmit("2-02")
        GP.output(ledOut, True)
        capture(3, (False, False, True), False)
        GP.output(ledOut, False)
        transmit("2-04")
        closeEvent = 0
    else:
        transmit("2-01")
        capture(2, (False, True, False), True)  # front cam
        transmit("2-03")

相关问题 更多 >