使用 Python 内置的 'concurrent.futures' 模块来创建线程池。具体代码实现如下:

import concurrent.futures

def function(arg1, arg2, arg3):
    # do something with the arguments
    pass

# create a list of arguments
args_list = [(1, 'a', True), (2, 'b', False), (3, 'c', True)]

# create a thread pool with 20 threads
with concurrent.futures.ThreadPoolExecutor(max_workers=20) as executor:
    # submit each task to the thread pool with one set of arguments
    futures = [executor.submit(function, *args) for args in args_list]

    # wait for all tasks to complete
    concurrent.futures.wait(futures)

在上述代码中,'function' 是需要执行的函数,'arg1'、'arg2' 和 'arg3' 是需要传递给函数的多个参数。'args_list' 是一个包含多组参数的列表。通过 'concurrent.futures.ThreadPoolExecutor' 创建一个最大线程数为 20 的线程池。然后使用 'executor.submit' 方法将每个任务提交到线程池,并传递相应的参数。最后,使用 'concurrent.futures.wait' 等待所有任务完成。

Python 多线程池:快速执行带多个参数的任务

原文地址: https://www.cveoy.top/t/topic/m0OH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录