实例1:简单实现单个任务多线程
from concurrent.futures import ThreadPoolExecutor, as_completed, ProcessPoolExecutor THREAD_POOL = ThreadPoolExecutor(4) # write为函数名,'打球为参数' def thread_write(): all_task = [] for _ in range(4): all_task.append(THREAD_POOL.submit(write, '打球')) for future in as_completed(all_task): # 子线程全部完成时 future.result() def write(moive): time.sleep(3) print(moive) time.sleep(2)
实例2:多个子任务多线程实现
from concurrent.futures import ThreadPoolExecutor import time from datetime import datetime # 参数times用来模拟网络请求的时间 def get_html(times): time.sleep(times) print("get page {}s finished {}".format(times,datetime.now())) return times def madd(*list): time.sleep(2) return sum(list) # wait的应用 executor = ThreadPoolExecutor(max_workers=2) # 通过submit函数提交执行的函数到线程池中,submit函数立即返回,不阻塞 task1 = executor.submit(get_html, *(3,)) # 开一个线程去处理事务 task2 = executor.submit(madd, *(2,3,4)) # 开第二个线程去处理事务 # done方法用于判定某个任务是否完成 print(task1.done()) # cancel方法用于取消某个任务,该任务没有放入线程池中才能取消成功 print(task2.cancel()) print(datetime.now()) time.sleep(4) print(datetime.now()) print(task1.done()) # result方法可以获取task的执行结果 print(task1.result()) print(task2.result()) wait(all_task, return_when=ALL_COMPLETED) # 等待子线程全部完成
实例3:多线程,当单线程完成时,能补充,时刻保持四线程运行
# 线程完成时,可以重复提交 from concurrent.futures import ThreadPoolExecutor,wait,ALL_COMPLETED,FIRST_COMPLETED import time from datetime import datetime # 参数times用来模拟网络请求的时间 def sleep_1(): time.sleep(1) print(f'{datetime.now()}:sleep 1s') def sleep_2(): time.sleep(2) print(f'{datetime.now()}:sleep 2s') def sleep_3(): time.sleep(3) print(f'{datetime.now()}:sleep 3s') def sleep_4(): time.sleep(4) print(f'{datetime.now()}:sleep 4s')
with ThreadPoolExecutor(max_workers=4) as t: taskDict = {'task_1':sleep_1,'task_2':sleep_2,'task_3':sleep_3,'task_4':sleep_4} # all_task.extend([t.submit(value) for value in taskDict.values()]) allTaskDict = {name:t.submit(runFunc) for name,runFunc in taskDict.items()} while 1: wait(allTaskDict.values(),return_when=FIRST_COMPLETED) # todo 这里判断是否需要添加 for name,task in allTaskDict.items(): if task.done(): # print(task.result()) # 重新提交线程 allTaskDict.update({name:t.submit(taskDict[name])})
使用:shutdown , 或用with来关闭线程.
pool.shutdown(wait=True)