多处理队列批处理最多可达 N 个元素 [英] Multiprocessing queue batch get up to max N elements

查看:45
本文介绍了多处理队列批处理最多可达 N 个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以阻塞方式从队列中获取尽可能多的项目(最多 N 个).例如:

I need to get as many items as I can from a queue (up to N), in a blocking fashion. e.g:

queue.get(16)

最多应返回 16 个元素,但如果为空则阻塞.

Should return up to 16 elements, but block if empty.

推荐答案

没有内置这样的工具,所以你需要自己编写代码;例如,

There's no such facility built in, so you'll need to code it yourself; for example,

import queue  # in Python 3; Queue in Python 2
...
def getn(q, n):
    result = [q.get()]  # block until at least 1
    try:  # add more until `q` is empty or `n` items obtained
        while len(result) < n:
            result.append(q.get(block=False))
    except queue.Empty:
        pass
    return result

然后为你的概念queue.get(16)getn(queue, 16).

这篇关于多处理队列批处理最多可达 N 个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆