检查队列.空()产生令人费解的行为

2024-09-30 00:41:55 发布

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

我用多个线程从多个不同采集率的传感器中提取数据。我有一个30Hz的摄像头和一个200Hz的多轴IMU。在收集数据的方法中,我做了一些基线处理,并将数据放入队列中供主代码使用。你知道吗

我主要从以下几点开始。camq、inerq和plotq都是排队。排队()对象。cammer、inertial和plotter都是具有无限循环的方法,用于收集数据并将其推入队列。你知道吗

t1 = Thread(target=cammer, args = (camq,))
t2 = Thread(target = inertial, args = (inerq,))
t3 = Thread(target = plotter, args = (plotq,))
t1.setDaemon(True)
t2.setDaemon(True)
t3.setDaemon(True)
t1.start()
t2.start()
t3.start()

我的应用程序的关键值是IMU值。因此,在我的主代码中的无限循环中,起搏项是读取这些值的队列。我的问题是我不想在代码继续执行之前等待摄像头队列填充。因此,我在主代码中完成了以下操作:

while True:
    result = inerq.get() #Get the inertial data as it comes

    #Many lines of inertial processing code

    if not camq.empty(): #Check if the camera queue is empty
        if camq.get_nowait() >297 and camq.get_nowait() <303: #If it isn't empty poll it to see if it meets the criteria.

            #Many lines of kalman filtering

我想使用queue.get\u nowait队列()所以我不会坐在那里等待我的相机数据,这比IMU数据来得慢。当我这么做的时候队列.空,所以我用队列.空()以上查询。你知道吗

当我这样做时,我得到错误“主线程不在主循环中”。值得注意的是,如果我这么做:

if camq.get() >297 and camq.get() <303: #Poll the camera data

            #Many lines of kalman filtering

代码运行得很好,但速度要慢得多,因为每次循环都在等待摄像头数据。你知道吗

有人能给我提供一个策略或见解,让我不“等待”的相机队列?你知道吗


Tags: the数据代码truegetif队列it

热门问题