Python:阻止对queue until条件的输入

2024-06-26 17:53:01 发布

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

我想并行图像读取,评估和csv写入过程。因此,我的想法是创建线程来加载图像并将它们放入队列(ImageQueue),然后由多个进程对其进行评估,这些进程从ImageQueue中提取图像,并将评估的数据放入另一个队列,在该队列中,通过写入线程来提取项目。我唯一的问题是,如果队列还不够大(比如说1000个图像/项目),那么图像加载线程应该只加载更多的图像。是否有某种方法可以用来等待/阻止,直到有足够的evaluate进程条目从ImageQueue中选取条目?某种缓冲?你知道吗

所以我的threadReading看起来像这样:

def threadReading(dfInputData, imageQueue):
    for row in dfInputData.iterrows():
        start = time.time()
        image = fh.loadImage(row['imageName'])
        end = time.time()
        timingLoadImage = end - start
        #########################################################################
        #Here somekind of conition should block/wait until queue is small enough#
        #########################################################################
        imageQueue.put(row, image, timingLoadImage)

Tags: 项目图像imagetime队列进程条目线程
1条回答
网友
1楼 · 发布于 2024-06-26 17:53:01
import queue

imageQueue = queue.Queue(maxsize=1000)
imageQueue.put(...)

它将在imageQueue.put(...)阻塞,直到队列中再次有空间为止。你知道吗

相关问题 更多 >