Python并行遍历一个列表? [英] Python iterating over a list in parallel?

查看:86
本文介绍了Python并行遍历一个列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建的列表中包含不超过8个项目,现在我用标准的for循环用于列表中的项目:"遍历该项目,我想知道是否有一种方法可以检查所有元素在同一时间?我看过一些有关使用zip同时遍历2个列表的帖子,这可能会有所帮助.

I have a list created with no more than 8 items in it, right now I loop over the item with a standard for loop "for item in list:" I am wondering if there is a way to check all of the elements at the exact same time? I have seen some posts about using zip to iterate over 2 lists at the same time which could help.

如果没有办法同时遍历一个列表元素,我想我需要将一个单独的列表拆分为4个单独的列表,然后使用zip遍历2个,在另一个线程上同时进行另外2个,但这似乎是一个笨拙的修复程序.

If there is no way to iterate over a lists elements at the same time I would guess I need to split one single list into 4 separate lists and iterate 2 at the same time using zip and do the other 2 on a separate thread, that seems like it could be a clunky fix though.

任何帮助将不胜感激.

修改 抱歉,我有一个清单

Edit Sorry to be vague, I have a list

apples = ['green', 'green', 'green', 'red']

现在我使用一个for循环遍历每个元素,

right now I use a for loop to go over each element,

for apple in apples:
    checkAppleColour(apple)
    #Do some more stuff depending on colour....

现在,它一个接一个地循环遍历苹果中的每个元素,我想做的是在每毫秒精确计数的同时,对每个项目进行一次颜色检查(使用的示例代码,但是原则与我要达到的目标完全相同).

right now it loops over each element in apple one by one, what I would like to be able to do is check each of the items at the exact same time for a colour check as every millisecond counts (example code used, but principle is exactly the same for what I am trying to achieve).

这里的性能是关键,我不介意是否必须使用它的线程,但是我需要最大的速度/效率来检查每个元素,我的猜测是并行性是做到这一点的方法,我只是不确定如何

Performance is key here I don't mind if its threading I have to use, but I need maximum speed / efficiency for checking each element and my guess is parallelism is the way to do it, I am just not quite sure how.

推荐答案

如果可以将函数映射到列表,则并行化很容易.

If you can map a function onto the list, parallelism is easy.

def addOne (x):
    return x + 1

默认映射在一个线程上执行操作.

The default map does things on one thread.

map(addOne, range(1, 4)) #When iterated over produces [2, 3, 4]

如果您的问题是地图,则意味着它是平行的.

If your problem is a map, it means that it's trivially parallel.

from multiprocessing import Pool

pool = Pool()
pool.map(addOne, range(1, 4)) ##When iterated over produces [2, 3, 4]

这使用多个过程. multiprocessing.dummy.Pool是线程池.请参阅这篇文章,以很好地介绍两者的原理.

This uses multiple processes. multiprocessing.dummy.Pool is a thread pool. See this article for a great introduction to the rationale behind both.

这篇关于Python并行遍历一个列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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