多处理队列批处理最多可达 N 个元素

编程入门 行业动态 更新时间:2024-10-08 05:27:53
本文介绍了多处理队列批处理最多可达 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 个元素

本文发布于:2023-11-30 15:17:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1650479.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最多   批处理   队列   可达   多处

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!