系统启动一个新线程的成本是比较高的,因为它涉及与操作系统的交互。在这种情形下,使用线程池可以很好地提升性能,尤其是当程序中需要创建大量生存期很短暂的线程时,更应该考虑使用线程池。
线程池在系统启动时即创建大量空闲的线程,程序只要将一个函数提交给线程池,线程池就会启动一个空闲的线程来执行它。当该函数执行结束后,该线程并不会死亡,而是再次返回到线程池中变成空闲状态,等待执行下一个函数。
此外,使用线程池可以有效地控制系统中并发线程的数量。当系统中包含有大量的并发线程时,会导致系统性能急剧下降,甚至导致 Python 解释器崩溃,而线程池的最大线程数参数可以控制系统中并发线程的数量不超过此数。
线程池的基类是 concurrent.futures 模块中的 Executor,Executor 提供了两个子类,即 ThreadPoolExecutor 和 ProcessPoolExecutor,其中 ThreadPoolExecutor 用于创建线程池,而 ProcessPoolExecutor 用于创建进程池。
如果使用线程池/进程池来管理并发编程,那么只要将相应的 task 函数提交给线程池/进程池,剩下的事情就由线程池/进程池来搞定。
Exectuor 提供了如下常用方法:
程序将 task 函数提交(submit)给线程池后,submit 方法会返回一个 Future 对象,Future 类主要用于获取线程任务函数的返回值。由于线程任务会在新线程中以异步方式执行,因此,线程执行的函数相当于一个“将来完成”的任务,所以 Python 使用 Future 来代表。
Future 提供了如下方法:
在用完一个线程池后,应该调用该线程池的 shutdown() 方法,该方法将启动线程池的关闭序列。调用 shutdown() 方法后的线程池不再接收新任务,但会将以前所有的已提交任务执行完成。当线程池中的所有任务都执行完成后,该线程池中的所有线程都会死亡。
使用线程池来执行线程任务的步骤如下:
下面程序示范了如何使用线程池来执行线程任务:
1 def test(value1, value2=None):2 print("%s threading is printed %s, %s"%(threading.current_thread().name, value1, value2))3 time.sleep(2)4 return 'finished'5 6 def test_result(future):7 print(future.result())8 9 if __name__ == "__main__":
10 import numpy as np
11 from concurrent.futures import ThreadPoolExecutor
12 threadPool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="test_")
13 for i in range(0,10):
14 future = threadPool.submit(test, i,i+1)
15
16 threadPool.shutdown(wait=True)
1 结果:
2
3 test__0 threading is printed 0, 1
4 test__1 threading is printed 1, 2
5 test__2 threading is printed 2, 3
6 test__3 threading is printed 3, 4
7 test__1 threading is printed 4, 5
8 test__0 threading is printed 5, 6
9 test__3 threading is printed 6, 7
前面程序调用了 Future 的 result() 方法来获取线程任务的运回值,但该方法会阻塞当前主线程,只有等到钱程任务完成后,result() 方法的阻塞才会被解除。
如果程序不希望直接调用 result() 方法阻塞线程,则可通过 Future 的 add_done_callback() 方法来添加回调函数,该回调函数形如 fn(future)。当线程任务完成后,程序会自动触发该回调函数,并将对应的 Future 对象作为参数传给该回调函数。
直接调用result函数结果
1 def test(value1, value2=None):2 print("%s threading is printed %s, %s"%(threading.current_thread().name, value1, value2))3 time.sleep(2)4 return 'finished'5 6 def test_result(future):7 print(future.result())8 9 if __name__ == "__main__":
10 import numpy as np
11 from concurrent.futures import ThreadPoolExecutor
12 threadPool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="test_")
13 for i in range(0,10):
14 future = threadPool.submit(test, i,i+1)
15 # future.add_done_callback(test_result)
16 print(future.result())
17
18 threadPool.shutdown(wait=True)
19 print('main finished')
1 结果:
2
3 test__0 threading is printed 0, 1
4 finished
5 test__0 threading is printed 1, 2
6 finished
7 test__1 threading is printed 2, 3
8 finished
去掉上面注释部分,调用future.add_done_callback函数,注释掉第16行
1 test__0 threading is printed 0, 12 test__1 threading is printed 1, 23 test__2 threading is printed 2, 34 test__3 threading is printed 3, 45 finished6 finished7 finished8 test__1 threading is printed 4, 59 test__0 threading is printed 5, 6
10 finished
另外,由于线程池实现了上下文管理协议(Context Manage Protocol),因此,程序可以使用 with 语句来管理线程池,这样即可避免手动关闭线程池,如上面的程序所示。
此外,Exectuor 还提供了一个 map(func, *iterables, timeout=None, chunksize=1) 方法,该方法的功能类似于全局函数 map(),区别在于线程池的 map() 方法会为 iterables 的每个元素启动一个线程,以并发方式来执行 func 函数。这种方式相当于启动 len(iterables) 个线程,井收集每个线程的执行结果。
例如,如下程序使用 Executor 的 map() 方法来启动线程,并收集线程任务的返回值:
示例换成多参数的:
def test(value1, value2=None):print("%s threading is printed %s, %s"%(threading.current_thread().name, value1, value2))
# time.sleep(2)if __name__ == "__main__":import numpy as npfrom concurrent.futures import ThreadPoolExecutorthreadPool = ThreadPoolExecutor(max_workers=4, thread_name_prefix="test_")for i in range(0,10):
# test(str(i), str(i+1))threadPool.map(test, [i],[i+1]) # 这是运行一次test的参数,众所周知map可以让test执行多次,即一个[]代表一个参数,一个参数赋予不同的值即增加[]的长度如从[1]到[1,2,3]threadPool.shutdown(wait=True)
上面程序使用 map() 方法来启动 4个线程(该程序的线程池包含 4 个线程,如果继续使用只包含两个线程的线程池,此时将有一个任务处于等待状态,必须等其中一个任务完成,线程空闲出来才会获得执行的机会),map() 方法的返回值将会收集每个线程任务的返回结果。
通过上面程序可以看出,使用 map() 方法来启动线程,并收集线程的执行结果,不仅具有代码简单的优点,而且虽然程序会以并发方式来执行 test() 函数,但最后收集的 test() 函数的执行结果,依然与传入参数的结果保持一致。