gpt4 book ai didi

python - 分组一些线程函数Python

转载 作者:行者123 更新时间:2023-12-03 13:08:40 25 4
gpt4 key购买 nike

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

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

最佳答案

有两个问题需要考虑:

  • 线程分组
  • 流量控制

  • 您可以手动使用 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中的 kwargsThread关键字参数。 concurrent.futures在Python3的标准库中,并且已移植到Python 2.5+,您可能需要执行 sudo pip install futures

    关于python - 分组一些线程函数Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47131980/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com