简体   繁体   English

如何使Python同时对列表中的所有项目执行循环?

[英]How to make Python do a loop for all items in list at the same time?

I have a list and I'm trying to do a loop for each item in the list, all at the same time. 我有一个列表,我想同时为列表中的每个项目做一个循环

I've tried using this code: 我尝试使用此代码:

thelist = ['first', 'second', 'third']

def loop():
    while True:
        for x in thelist:
            x = str(x)
            time.sleep(5)
            do_stuff_that_includes_x()

But it does the stuff in the loop one by one as sorted in thelist . 但它确实通过一个作为排序中环的东西thelist

And I want it to do the stuff for all items in thelist at the same time . 我想要它做的东西,对所有项目thelist 在同一时间

Thanks in advance. 提前致谢。

As has been noted in the comments by vossad01, your code has a 5 seconds delay inside your loop. 正如vossad01的注释所指出的那样,您的代码在循环内有5秒的延迟。 This will cause a five second delay between any two items on the list. 这将导致列表中任何两个项目之间延迟五秒钟。 If you remove the 5 second delay, your messages will be sent to all rooms in the list near-instantaneous. 如果您取消5秒延迟,您的消息将立即发送到列表中的所有房间。

thelist = ['first', 'second', 'third']

def loop():
    while True:
        for x in thelist:
            x = str(x)
            do_stuff_that_includes_x() 

        time.sleep(5)

I think you need multi-processing: 我认为您需要多重处理:

import time

def work(x):
    x = str(x)
    time.sleep(5)
    print x
#   do_stuff_that_includes_x()

thelist = ['first', 'second', 'third']
from multiprocessing import Pool
p = Pool( len( thelist ) )
p.map( work, thelist )

First, multithreaded parallelization does not tend to yield performance increases because of the Global Interpreter Lock (GIL). 首先,由于全局解释器锁(GIL),多线程并行化不会使性能提高。 Thus, if you are doing this for performance reasons you will need to look at the multiprocessing module. 因此,如果出于性能原因执行此操作,则需要查看多处理模块。 See how do I parallelize a simple python loop? 看看如何并行化一个简单的python循环? for an example of using the map member of of a process pool to accomplish this. 有关使用进程池的映射成员完成此操作的示例。

Side notes: It is bad form to re-assign the iterating variable (x). 注意:重新分配迭代变量(x)是一种不好的形式。 Additionally, since you want parallel execution it will be easiest if you can make do_stuff_that_includes_x() parametrized on x. 另外,由于您希望并行执行,因此如果可以在x上设置do_stuff_that_includes_x()参数,将是最简单的。

使用*运算符一次解压缩整个列表

do_stuff_that_includes_x(*x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使gif始终在python中循环? - How to make gif loop all the time in python? 如何使用龟在python中同时绘制项目? - How to make items draw at the same time in python using turtle? 如何进行循环以从列表中获取特定项目? - python - how to make a loop to get specific items from a list? - python 我将如何做到这一点,以便在查找列表的所有可能组合时只能同时使用某些项目 - How would I make it so only some items are able to be used at the same time when finding all possible combinations of a list 如何使我所有的for循环同时运行 - How to make all of my for loop runs at the same time 如何将 append 项目添加到 python 中的 for 循环内的列表中? - How do I append items to a list inside a for loop in python? 如何在Python的同一列表中比较和分组等效项? - How do I compare and group equivalent items in the same list in Python? Python列表:检查所有项目是否相同 - Python list: check if all items are the same or not 刚开始学习 python; 当我使用列表推导时,为什么这个 for 循环的打印不同? 我如何使循环相同? - New to learning python; why is the print for this for-loop different when I use a list comprehension? how do i make the loop be the same? 在 Python 中,如何将列表中的所有项目转换为浮点数? - In Python, how do I convert all of the items in a list to floats?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM