你是否听到人们说过,异步 Python 代码比"普通(或同步)Python 代码更快?果真是那样吗? 1 "同步"和"异步"是什么意思? Web 应用程序通常要处理许多请求,这些请求在很短的时间段内来自不同的客户端.为避免处理延迟,必须考虑并行处理多个请求,这通常称为"并发". 在本文中,我将继续使用 Web 应用程序作为例子,但是要记住还有其它类型的应用程序也从并发完成多个任务中获益,因此这个讨论并不仅仅是针对 Web 应用程序的.
python中ThreadPoolExecutor(线程池)与ProcessPoolExecutor(进程池)都是concurrent.futures模块下的,主线程(或进程)中可以获取某一个线程(进程)执行的状态或者某一个任务执行的状态及返回值. 通过submit返回的是一个future对象,它是一个未来可期的对象,通过它可以获悉线程的状态 ThreadPoolExecutor(线程池) 通过submit函数提交执行的函数到线程池中,done()判断线程执行的状态: import time f