组合一些线程函数Python

时间:2017-11-06 07:41:03

标签: python multithreading grouping

我有一些功能,我希望它们在不同的线程组中。也就是说,我希望那些不同的线程组能够一个接一个地运行。我的示例代码如下:

import threading
from threading import Thread

def func_a():
    # do something here


def func_b():
    # do something here

def func_c():
    # do something here

def func_d():
    # do something here

thread_a = threading.Thread(target = func_a)
thread_b = threading.Thread(target = func_b)
thread_c = threading.Thread(target = func_c)
thread_d = threading.Thread(target = func_d)

thread_a.start()
thread_b.start()
thread_c.start()
thread_d.start()

thread_a.join()
thread_b.join()
thread_c.join()
thread_d.join()

我想要做的非常简单:将func_afunc_b设为threading_group_a,同时将func_cfunc_d设为threading_group_b }。然后先执行threading_group_a,然后threading_group_b

2 个答案:

答案 0 :(得分:0)

您可以使用Pool。这是将线程组合在一起的方法,并为它们分配一个任务。然后,您将任务分配给池,而不是特定的线程。你有4个功能,但它们只做两件事。所以,你可以这样做:

from multiprocessing.pool import ThreadPool

def func_a(*args):
    print 1

def func_b(*args):
    print 2

pool = ThreadPool(3)

pool.map(func_a, range(2))
pool.map(func_b, range(2))

11

22

请注意,我必须更改函数签名,因为pool.map将一些args传递给函数。文档指定地图块,直到结果准备好,因此我们可以删除.join()

更新:是的,你可以。假设你需要一个+ b来一起执行。你可以将它们包装在第三个​​函数中,它所做的就是调用另外两个函数:

def ab(*args):
    func_a()
    func_b()

现在您将ab函数传递给线程池。如果需要从函数返回值,则将返回值分配给列表并返回该列表。

更新2:

from multiprocessing.pool import ThreadPool

def func_a():
    print 1

def func_b():
    print 2

def ab(*args):
    func_a()
    func_b()

pool = ThreadPool(3)
pool.map(ab, range(2))

答案 1 :(得分:0)

有两个问题需要考虑:

  1. 主题分组
  2. 流量控制
  3. 您可以采用手动方式,使用listtuple对参数和线程本身进行分组,并手动控制流程:

    from threading import Thread
    
    # Functions for group 1: with no arguments and different fucntions
    def func_a():
        print(1)
    
    def func_b():
        print(1)
    
    # Functions for group 2: with a single function but different arguments
    def func_c(i):
        print(i)
    
    # Functions for group 3: with different functions and arguments
    def func_d(i):
        print(i)
    
    def func_e(i):
        print(i)
    
    funs_1 = (func_a, func_b)
    args_2 = ((2,), (2,))
    funs_3 = (func_d, func_e)
    args_3 = ((3,), (3,))
    
    threads_1 = tuple(Thread(target=func             ) for func in funs_1)
    threads_2 = tuple(Thread(target=func_c, args=args) for args in args_2)
    threads_3 = tuple(Thread(target=func,   args=args) for func, args in zip(funs_3, args_3))
    
    for thread in threads_1:
        thread.start()
    
    for thread in threads_1:
        thread.join()
    
    for thread in threads_2:
        thread.start()
    
    for thread in threads_2:
        thread.join()
    
    for thread in threads_3:
        thread.start()
    
    for thread in threads_3:
        thread.join()
    

    或者使用池,允许您指定可以启动的并发线程的数量,并且还将处理控件:

    from concurrent.futures import ThreadPoolExecutor
    
    # Functions for group 1: with no arguments and different fucntions
    def func_a():
        print(1)
    
    def func_b():
        print(1)
    
    # Functions for group 2: with a single function but different arguments
    def func_c(i):
        print(i)
    
    # Functions for group 3: with different functions and arguments
    def func_d(i):
        print(i)
    
    def func_e(i):
        print(i)
    
    funs_1 = (func_a, func_b)
    args_2 = ((2,), (2,))
    funs_3 = (func_d, func_e)
    args_3 = ((3,), (3,))
    
    with ThreadPoolExecutor(max_workers=5) as pool:
        futures_1 = tuple(pool.submit(func) for func in funs_1)
    
    with ThreadPoolExecutor(max_workers=5) as pool:
        futures_2 = tuple(pool.submit(func_c, *args) for args in args_2)
    
    with ThreadPoolExecutor(max_workers=5) as pool:
        futures_3 = tuple(pool.submit(func, *args) for func, args in zip(funs_3, args_3))
    

    with语句确保池在退出之前完成所有任务。您只需要调用ThreadPoolExecutor实例方法submit()作为target关键字参数等效的第一个参数,其余的位置参数和关键字参数将传递给函数,如{来自args的{​​1}}和kwargs个关键字参数。 Thread位于Python3的标准库中,并已移植到Python 2.5+,您可能需要执行concurrent.futures