简体   繁体   English

如何在python中的单个函数下并行运行多个for循环

[英]How to run multiple for loops in parallel under a single function in python

Is there a way to have multithreading implemented for multiple for loops under a single function. 有没有一种方法可以在单个函数下为多个for循环实现多线程。 I am aware that it can be achieved if we have separate functions, but is it possible to have it under the same function. 我知道,如果我们有单独的功能,则可以实现,但是可以在相同的功能下实现。 For example: 例如:

def sqImport():
    for i in (0,50):
        do something specific to 0-49
    for i in (50,100):
        do something specific to 50-99
    for i in (100,150):
        do something specific to 100-149

If there are 3 separate functions for 3 different for loops then we can do: 如果针对3个不同的for循环有3个单独的函数,那么我们可以这样做:

threadA = Thread(target = loopA)
threadB = Thread(target = loopB)
threadC = Thread(target = loopC)
threadA.run()
threadB.run()
threadC.run()
# Do work indepedent of loopA and loopB 
threadA.join()
threadB.join()
threadC.join()

But is there a way to achieve this under a single function? 但是,有一种方法可以在单个功能下实现这一目标吗?

First of all: I think you really should take a look at multiprocessing.ThreadPool if you are going to use it in a productive system. 首先:我想您真的应该看一下multiprocessing.ThreadPool如果您打算在生产系统中使用它。 What I describe below is just a possible workaround (which might be simpler and therefore could be used for testing purposes). 我在下面描述的只是一个可能的解决方法(它可能更简单,因此可以用于测试目的)。

You could pass an id to the function and use that to decide which loop you take like so: 您可以将一个id传递给该函数,并使用它来确定要执行的循环,如下所示:

from threading import Thread

def sqImport(tId):
    if tId == 0:
        for i in range(0,50):
            print i
    elif tId == 1:
        for i in range(50,100):
            print i
    elif tId == 2:
        for i in range(100,150):
            print i

threadA = Thread(target = sqImport, args=[0])
threadB = Thread(target = sqImport, args=[1])
threadC = Thread(target = sqImport, args=[2])
threadA.start()
threadB.start()
threadC.start()
# Do work indepedent of loopA and loopB 
threadA.join()
threadB.join()
threadC.join()

Note that I used start() instead of run() because run() does not start a different thread but executes in the current thread context. 请注意,我使用start()而不是run()因为run()不会启动其他线程,而是在当前线程上下文中执行。 Moreover I changed your for i in (x, y) loops in for i in range(x,y) loops, because I think, You want to iterate over a range and not a tuple(that would iterate only over x and y). 而且我for i in (x, y)循环中将for i in range(x,y)更改for i in (x, y) for i in range(x,y) for i in (x, y)循环中for i in range(x,y) ,因为我认为,您要遍历一个范围而不是一个元组(仅遍历x和y) 。


An alternative Solution using multiprocessing might look like this: 使用multiprocessing 的替代解决方案可能如下所示:

from multiprocessing.dummy import Pool as ThreadPool

# The worker function
def sqImport(data):
    for i in data:
        print i


# The three ranges for the three different threads
ranges = [
    range(0, 50),
    range(50, 100),
    range(100, 150)
    ]

# Create a threadpool with 3 threads
pool = ThreadPool(3)
# Run sqImport() on all ranges
pool.map(sqImport, ranges)

pool.close()
pool.join()

You can use multiprocessing.ThreadPool which will divide you tasks equally between running threads. 您可以使用multiprocessing.ThreadPool ,它将在运行线程之间平均分配任务。 Follow Threading pool similar to the multiprocessing Pool? 遵循类似于多处理池的线程池? for more on this. 有关此的更多信息。

If you are really looking for parallel execution then go for processes because threads will face python GIL(Global Interpreted Lock). 如果您真的在寻找并行执行,那就去找进程,因为线程将面对python GIL(全局解释锁)。

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

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