我正在使用gpiozero python 库来处理 Raspberry Pi 上的简单 GPIO 设备(我在这里使用 MotionSensor 作为示例):
import asyncio
from gpiozero import MotionSensor
class MotionSensorHandler():
__whenMotionCallback = None
def __init__(self, pin, whenMotionCallback):
# whenMotionCallback is an async function
self.__whenMotionCallback = whenMotionCallback
# Just init the sensor with gpiozero lib
motionSensor = MotionSensor(pin)
# Method to call when motion is detected
motionSensor.when_motion = self.whenMotion
async def whenMotion(self):
await self.__whenMotionCallback()
我的问题是我试图给一个async
函数回调到motionSensor.when_motion
.
所以我得到了这个whenMotion
函数的错误,async
但从来没有await
,但我实际上不能等待它:
# will not work because MotionSensor() is not using asyncio
motionSensor.when_motion = await self.whenMotion
您知道如何将我的async
功能分配给 none one 吗?