【发布时间】:2023-06-01 12:46:01
【问题描述】:
我是 Python 异步编程的新手,互联网并没有帮助我解决我的问题 - 你们中有人有解决方案吗?
我有一个无限循环,其中读取了一些传感器数据。但是,传感器读数很慢,所以我想等待传感器信号。
我对它的期望是这样的(只是示意图):
import bno055 #sensor library
import asyncio
aync def read_sensor():
altitude=bno055.read()
#..and some other unimportant lines which I hide here
return altitude
def main():
while 1:
await current_altitude= read_sensor() #??? how can I "await" the sensor signals?
#....some other lines which I hide here, but they need to run syncronously
print(current_altitude)
main()
提前谢谢你
【问题讨论】:
-
如果
bno055.read不是异步的,你需要以某种方式将它包装在一个线程或类似的线程中。
标签: python-3.x asynchronous async-await raspberry-pi3 sensors