Python线程参数必须是int,或具有fileno()方法

2024-04-19 19:50:12 发布

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

当我在Raspberry Pi上执行下面的代码时,它主要工作,并在应该的时候打印灯光和运动检测消息,但在输出中,我不断得到以下错误:

argument must be an int, or have a fileno() method

没有回溯,也没有尝试,除了子句似乎抓住了它

import time
import threading
import grovepi


def notify(msg):
    print(msg)
    buzzer.alert(.25)


class Buzzer:
    """ Buzzer class for trigger buzzer sounds. Accepts a timing pin """
    def __init__(self, pin, **kwargs):
        self.pin = pin
        grovepi.pinMode(self.pin, "OUTPUT")
        super(Buzzer, self).__init__(**kwargs)

    def alert(self, timing):
        grovepi.digitalWrite(self.pin, 1)
        time.sleep(timing)
        grovepi.digitalWrite(self.pin, 0)
        time.sleep(timing)


class LightSensorThread(threading.Thread):
    """
        Light sensor thread, monitors light sensor.
        Accepts Light Sensor Pin ID and light threshold before alerting
    """
    def __init__(self, pin, threshold=12, **kwargs):
        self.pin = pin
        self.threshold = threshold
        super(LightSensorThread, self).__init__(**kwargs)

    def run(self):
        grovepi.pinMode(self.pin, "INPUT")

        while True:
            try:
                sensor_value = grovepi.analogRead(self.pin)
                if sensor_value:
                    resistance = (float)(1023 - sensor_value) * 10 / sensor_value

                    if resistance > self.threshold:
                        notify('Light Detected - {0}!'.format(sensor_value))
                    time.sleep(.5)
            except Exception as err:
                print("Light Error", err)


class PIRSensorThread(threading.Thread):
    """
        PIR Sensor monitoring thread, accepts PIR Sensor pin
    """
    def __init__(self, pin, **kwargs):
        self.pin = pin
        super(PIRSensorThread, self).__init__(**kwargs)

    def run(self):
        grovepi.pinMode(self.pin, "INPUT")

        while True:
            try:
                if grovepi.digitalRead(self.pin):
                    notify('Motion Detected')

                time.sleep(.3)
            except Exception as err:
                print("Motion Error", err)


if __name__ == "__main__":
    LIGHT_PIN = 0
    MOTION_PIN = 8
    BUZZ_PIN = 4
    buzzer = Buzzer(BUZZ_PIN)
    LightSensorThread(LIGHT_PIN, 20).start()
    PIRSensorThread(MOTION_PIN).start()

因此,即使我没有更多的话要说,也要我在问题中添加更多的文本,但是我已经尝试过用额外的尝试来围绕每个不同的代码块,除了错误捕获块,但没有成功


Tags: selfthresholdtimeinitvaluedefpinsleep
1条回答
网友
1楼 · 发布于 2024-04-19 19:50:12

对于后来遇到这个问题的人,我也看到了这个信息。对我来说,常见的因素是导入旧的Dexter Industries代码,不是grovepi,而是easygopigo3代码来访问距离传感器和互斥保护

这些消息似乎来自DI代码打印消息并执行异常

显然,尤其是在grovepi上,因为grovepi方法不使用互斥保护-请参见https://forum.dexterindustries.com/t/grove-gps-sensor-slow-to-read/7609/10?u=cyclicalobsessive

https://dexterind.github.io/GrovePi/api/gpio/

IMPORTANT

This library and the other ones too are not thread-safe. You cannot call the GrovePi from multiple threads or processes as that will put the GrovePi into a broken state.

In case you need to reset the GrovePi from your Raspberry Pi, check this section.

The functions don't verify if the input parameters are valid and therefore the parameters have to be verified/validated before that. Calling a function with improper parameters can result in an undefined behavior for the GrovePi.

相关问题 更多 >