说明
Python3.2开始,标准库为我们提供了concurrent.futures模块,它提供了ThreadPoolExecutor和ProcessPoolExecutor两个类,实现了对threading和multiprocessing的进一步抽象,对编写线程池/进程池提供了直接的支持.
代码测试
#! /usr/bin/env python
# -*- coding: utf-8 -*-#
# -------------------------------------------------------------------------------
# Name: demo3
# Author: yunhgu
# Date: 2021/7/8 15:17
# Description:
# -------------------------------------------------------------------------------
import os
import time
import threading
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed
def work(x):
time.sleep(1)
temp = f"父进程{os.getppid()}:子进程{os.getpid()}:线程{threading.get_ident()}:{x}"
return temp
def sub_thread():
temp_list = []
with ThreadPoolExecutor(max_workers=3) as t:
task_list = [t.submit(work, i) for i in range(5)]
for task in as_completed(task_list):
if task.done():
temp_list.append(task.result())
return temp_list
def main():
print(f"主进程:{os.getpid()}")
path_list = []
with ProcessPoolExecutor(max_workers=3) as p:
task_list = [p.submit(sub_thread) for i in range(5)]
for task in as_completed(task_list):
if task.done():
path_list.append(task.result())
for path in path_list:
print(path)
if __name__ == '__main__':
main()