How to use queue with concurrent future ThreadPoolExecutor in python 3?(如何在 python 3 中将队列与并发未来的 ThreadPoolExecutor 一起使用?)
问题描述
我正在使用简单的线程模块来执行并发作业.现在我想利用并发期货模块.有人能给我举一个使用队列和并发库的例子吗?
I am using simple threading modules to do concurrent jobs. Now I would like to take advantages of concurrent futures modules. Can some put me a example of using a queue with concurrent library?
我收到 TypeError: 'Queue' object is not iterable我不知道如何迭代队列
I am getting TypeError: 'Queue' object is not iterable I dont know how to iterate queues
代码片段:
def run(item):
self.__log.info(str(item))
return True
<queue filled here>
with concurrent.futures.ThreadPoolExecutor(max_workers = 100) as executor:
furtureIteams = { executor.submit(run, item): item for item in list(queue)}
for future in concurrent.futures.as_completed(furtureIteams):
f = furtureIteams[future]
print(f)
推荐答案
我会建议这样的事情:
def run(queue):
item = queue.get()
self.__log.info(str(item))
return True
<queue filled here>
workerThreadsToStart = 10
with concurrent.futures.ThreadPoolExecutor(max_workers = 100) as executor:
furtureIteams = { executor.submit(run, queue): index for intex in range(workerThreadsToStart)}
for future in concurrent.futures.as_completed(furtureIteams):
f = furtureIteams[future]
print(f)
您将遇到的问题是,队列被认为是无止境的,并且作为一种媒介来解耦将某些内容放入队列的线程和将项目从队列中取出的线程.
The problem you will run in is that a queue is thought to be endless and as a medium to decouple the threads that put something into the queue and threads that get items out of the queue.
当
- 您的商品数量有限或
- 您一次计算所有项目
然后并行处理它们,队列没有意义.在这些情况下,ThreadPoolExecutor 会使队列过时.
and afterwards process them in parallel, a queue makes no sense. A ThreadPoolExecutor makes a queue obsolete in these cases.
我查看了 ThreadPoolExecutor 源代码:
I had a look at the ThreadPoolExecutor source:
def submit(self, fn, *args, **kwargs): # line 94
self._work_queue.put(w) # line 102
里面使用了一个队列.
这篇关于如何在 python 3 中将队列与并发未来的 ThreadPoolExecutor 一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 python 3 中将队列与并发未来的 ThreadPoolExecutor 一起使用?
基础教程推荐
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01