python中起多线程池20个需要传递多个参数
可以使用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等待所有任务完成。
原文地址: https://www.cveoy.top/t/topic/bwUl 著作权归作者所有。请勿转载和采集!