简体   繁体   English

如何在两个列表上同时运行一个函数?

[英]How can I run a function on two lists at the same time?

I've got a simple question. 我有一个简单的问题。 Essentially I want to run a function on a list of lists at the same time or in parallel. 本质上,我想同时或并行在列表列表上运行一个函数。

Here is essentially what I'm working with 这实际上是我正在使用的

def func(x):
    print(x)
for objectList in space:
    for subObject in objectList:
        func(subObject)

I'd ideally like to be able to have func() run on every objectList at once. 理想情况下,我希望能够一次在每个objectList上运行func() I know (actually I don't, which is why I'm asking how to do this) this is threading or parallelism but this is my first attempt at such. 我知道(实际上我不知道,这就是为什么我问如何做到这一点),这是线程化或并行性,但这是我第一次尝试这样做。

My question is, is there a module or built in function in python or standard way to accomplish this task? 我的问题是,是否有模块或内置函数以python或标准方式完成此任务?

And sorry if I somehow broke a rule on this site or something or if you think this question is stupid or a duplicate or whatever. 很抱歉,如果我以某种方式违反了该站点上的规则或其他内容,或者您​​认为这个问题很愚蠢,重复或其他问题。

From the python docs: 从python文档中:

the Pool object [...] offers a convenient means of parallelizing the execution of a function across multiple input values, distributing the input data across processes (data parallelism) 池对象提供了一种方便的方法,可以跨多个输入值并行执行函数,跨进程分配输入数据(数据并行性)

You could use this pattern, which is also from the docs : 您可以使用此模式,该模式也来自docs

from multiprocessing import Pool

def f(x):
    return x*x

if __name__ == '__main__':
    with Pool(5) as p:
        print(p.map(f, [1, 2, 3]))

As pointed out by others you should have a valid point to use threading and parellel computting as it also requiers other actions from you like input/load distribution and managing workers (working threads). 正如其他人指出的那样,使用线程和并行计算应该是有道理的,因为它还需要您执行其他操作,例如输入/负载分配和管理工作程序(工作线程)。 There are certainly libraries that have built in those mechanics. 这些机制中肯定有内置的库。 For simple applications you can just use (threading.Thread) class. 对于简单的应用程序,您可以只使用(threading.Thread)类。 Here's a simple example of using threads. 这是使用线程的简单示例。 This code will create worker for every element of space. 此代码将为空间的每个元素创建工作程序。

import threading


def func(x):
    print(x)


class FuncThread(threading.Thread):
    def __init__(self, x):
        threading.Thread.__init__(self)
        self.x = x

    def run(self):
        #worker code = func(x)
        print('doing stuff with:', self.x)


space = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]
worker_handlers = list()

for objectList in space:
    for subObject in objectList:
        worker_handlers.append(FuncThread(subObject))

for worker in worker_handlers:
    worker.start()

print('The main program continues to run in foreground.')

for worker in worker_handlers:
    worker.join()  # Wait for the background tasks to finish
print('Main program waited until background was done.')

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM