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



>当我在树莓派上执行下面的代码时,它主要工作并在应该的时候打印灯光和运动检测消息,但在输出中混合时,我不断收到此错误:

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

SO 让我在问题中添加更多文本,即使我没有更多要说的,但我尝试用额外的 try-except 错误捕获块包围每个不同的代码块,但没有成功。

对于遇到此问题的人 - 我也看到此消息。对我来说,共同的因素是导入旧的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.

相关内容

最新更新