python multithreading等到所有线程都完成了

时间:2012-08-15 11:44:54

标签: python multithreading

这可能是在类似的背景下提出的,但在搜索约20分钟后我无法找到答案,所以我会问。

我编写了一个Python脚本(比方说:scriptA.py)和一个脚本(比如说scriptB.py)

在scriptB中我想用不同的参数多次调用scriptA,每次运行大约需要一个小时,(它是一个巨大的脚本,做了很多东西......不要担心它)我希望能够同时运行带有所有不同参数的scriptA,但我需要等到所有这些都完成后再继续;我的代码:

import subprocess

#setup
do_setup()

#run scriptA
subprocess.call(scriptA + argumentsA)
subprocess.call(scriptA + argumentsB)
subprocess.call(scriptA + argumentsC)

#finish
do_finish()

我想同时运行所有subprocess.call(),然后等到它们全部完成后,我该怎么做?

我尝试使用类似示例here

的线程
from threading import Thread
import subprocess

def call_script(args)
    subprocess.call(args)

#run scriptA   
t1 = Thread(target=call_script, args=(scriptA + argumentsA))
t2 = Thread(target=call_script, args=(scriptA + argumentsB))
t3 = Thread(target=call_script, args=(scriptA + argumentsC))
t1.start()
t2.start()
t3.start()

但我不认为这是对的。

在进入do_finish()之前,我怎么知道他们已经完成了所有工作?

8 个答案:

答案 0 :(得分:146)

将线程放入列表中,然后使用Join method

 threads = []

 t = Thread(...)
 threads.append(t)

 ...repeat as often as necessary...

 # Start all threads
 for x in threads:
     x.start()

 # Wait for all of them to finish
 for x in threads:
     x.join()

答案 1 :(得分:123)

您需要在脚本末尾使用Thread对象的join方法。

t1 = Thread(target=call_script, args=(scriptA + argumentsA))
t2 = Thread(target=call_script, args=(scriptA + argumentsB))
t3 = Thread(target=call_script, args=(scriptA + argumentsC))

t1.start()
t2.start()
t3.start()

t1.join()
t2.join()
t3.join()

因此主线程将等到t1t2t3完成执行。

答案 2 :(得分:23)

我更喜欢使用基于输入列表的列表理解:

inputs = [scriptA + argumentsA, scriptA + argumentsB, ...]
threads = [Thread(target=call_script, args=(i)) for i in inputs]
[t.start() for t in threads]
[t.join() for t in threads]

答案 3 :(得分:19)

在Python3中,由于Python 3.2有一种新的方法可以达到相同的结果,我个人更喜欢传统的线程创建/开始/加入,包concurrent.futureshttps://docs.python.org/3/library/concurrent.futures.html

使用ThreadPoolExecutor代码将是:

from concurrent.futures.thread import ThreadPoolExecutor
import time

def call_script(ordinal, arg):
    print('Thread', ordinal, 'argument:', arg)
    time.sleep(2)
    print('Thread', ordinal, 'Finished')

args = ['argumentsA', 'argumentsB', 'argumentsC']

with ThreadPoolExecutor(max_workers=2) as executor:
    ordinal = 1
    for arg in args:
        executor.submit(call_script, ordinal, arg)
        ordinal += 1
print('All tasks has been finished')

上一代码的输出类似于:

Thread 1 argument: argumentsA
Thread 2 argument: argumentsB
Thread 1 Finished
Thread 2 Finished
Thread 3 argument: argumentsC
Thread 3 Finished
All tasks has been finished

其中一个优点是您可以控制吞吐量设置最大并发工作者。

答案 4 :(得分:5)

您可以使用类似下面的类,您可以在其中添加'n'个函数或者想要以并行激情执行的console_scripts并开始执行并等待所有作业完成..

from multiprocessing import Process

class ProcessParallel(object):
    """
    To Process the  functions parallely

    """    
    def __init__(self, *jobs):
        """
        """
        self.jobs = jobs
        self.processes = []

    def fork_processes(self):
        """
        Creates the process objects for given function deligates
        """
        for job in self.jobs:
            proc  = Process(target=job)
            self.processes.append(proc)

    def start_all(self):
        """
        Starts the functions process all together.
        """
        for proc in self.processes:
            proc.start()

    def join_all(self):
        """
        Waits untill all the functions executed.
        """
        for proc in self.processes:
            proc.join()


def two_sum(a=2, b=2):
    return a + b

def multiply(a=2, b=2):
    return a * b


#How to run:
if __name__ == '__main__':
    #note: two_sum, multiply can be replace with any python console scripts which
    #you wanted to run parallel..
    procs =  ProcessParallel(two_sum, multiply)
    #Add all the process in list
    procs.fork_processes()
    #starts  process execution 
    procs.start_all()
    #wait until all the process got executed
    procs.join_all()

答案 5 :(得分:2)

也许,像

for t in threading.enumerate():
    if t.daemon:
        t.join()

答案 6 :(得分:1)

我遇到了同样的问题,我需要等待使用for循环创建的所有线程。我只是尝试了下面的代码。它可能不是完美的解决方案,但我认为它会是一个简单的测试解决方案:

for t in threading.enumerate():
    try:
        t.join()
    except RuntimeError as err:
        if 'cannot join current thread' in err:
            continue
        else:
            raise

答案 7 :(得分:1)

threading module documentation

  

有一个“主线程”对象;这对应于初始   Python程序中的控制线程。它不是守护进程线程。

     

可能会创建“虚拟线程对象”。   这些是与“外来线程”相对应的线程对象,它们是   控制线程从线程模块外部启动,例如   直接从C代码开始。虚拟线程对象的功能有限。   它们始终被认为是活动的和守护程序的,并且不能被join()编辑。   它们永远不会被删除,因为不可能检测到   终止外来线程。

因此,要在不希望保留所创建线程的列表的情况下捕获这两种情况:

import threading as thrd


def alter_data(data, index):
    data[index] *= 2


data = [0, 2, 6, 20]

for i, value in enumerate(data):
    thrd.Thread(target=alter_data, args=[data, i]).start()

for thread in thrd.enumerate():
    if thread.daemon:
        continue
    try:
        thread.join()
    except RuntimeError as err:
        if 'cannot join current thread' in err.args[0]:
            # catchs main thread
            continue
        else:
            raise

其中:

>>> print(data)
[0, 4, 12, 40]