Python 每次为数组中的每十项运行十个线程
我有一个数组,例如100个项目,我想为数组中的每10个项目运行10个线程 我的问题是,我不知道如何使用for循环为第一个10项、第二个10项等运行10个线程 我需要的是如下所示:Python 每次为数组中的每十项运行十个线程,python,arrays,multithreading,for-loop,Python,Arrays,Multithreading,For Loop,我有一个数组,例如100个项目,我想为数组中的每10个项目运行10个线程 我的问题是,我不知道如何使用for循环为第一个10项、第二个10项等运行10个线程 我需要的是如下所示: for item in myarray: thread.start_new_thread(first_item) thread.start_new_thread(second_item) thread.start_new_thread(third_item) thread.start_n
for item in myarray:
thread.start_new_thread(first_item)
thread.start_new_thread(second_item)
thread.start_new_thread(third_item)
thread.start_new_thread(fourth_item)
thread.start_new_thread(fifth_item)
thread.start_new_thread(sixth_item)
thread.start_new_thread(seventh_item)
thread.start_new_thread(eight_item)
thread.start_new_thread(ninth_item)
thread.start_new_thread(tenth_item)
对于for循环的第二轮,运行第二个10项的线程
如何每次将for循环的索引增加十倍?这是一个值得怀疑的设计,因为: 为每个项目启动一个新线程,而不是重复使用它们。创建线程的成本很高 您没有解释如何同步线程以将其数量限制为10个您真的要等待第一批10个线程完成后再开始10个吗?
常用的方法是使用线程池。您可以在concurrent.futures中搜索Python 3.2+,也可以在不太旧的旧版本中搜索。。。多处理.pool.ThreadPool.谢谢@Serge,我完全搞糊涂了。我只是想加速我的程序,但这是我第一次想在程序中使用线程。我认为,期货对我来说有点复杂: