python threadpoolexecutor_python 线程池 ThreadPoolExecutor 的用法
1. 线程池的基本用法
# coding: utf-8
from concurrent.futures import ThreadPoolExecutor
import time
def spider(page):
time.sleep(page)
print(f"crawl task{page} finished")
return page
with ThreadPoolExecutor(max_workers=5) as t: # 创建一个最大容纳数量为5的线程池
task1 = t.submit(spider, 1)
task2 = t.submit(spider, 2) # 通过submit提交执行的函数到线程池中
task3 = t.submit(spider, 3)
print(f"task1: {task1.done()}") # 通过done来判断线程是否完成
print(f"task2: {task2.done()}")
print(f"task3: {task3.done()}")
time.sleep(2.5)
print(f"task1: {task1.done()}")
print(f"task2: {task2.done()}")
print(f"task3: {task3.done()}")
print(task1.result()) # 通过result来获取返回值
executor = ThreadPoolExecutor(max_workers=5)
all_task = [executor.submit(send_data,"a" ) for i in range(1,2)]
'''
上面的代码等价于下面的:
all_task = []
for i in range(1,2):
task = executor.submit(send_data,"b" ) # "b"是给 方法send_data 传参,如果send_data有多个参数, executor.submit(send_data,"b","c","d" )
all_task.append(task)
'''
for future in as_completed(all_task):
print("finish the task")
obj_data = future.result()
print("obj_data is ", obj_data)
def send_data(a):
print("a is ", a)
as_completed:
上面虽然提供了判断任务是否结束的方法,但是不能在主线程中一直判断啊。最好的方法是当某个任务结束了,就给主线程返回结果,而不是一直判断每个任务是否结束。
ThreadPoolExecutorThreadPoolExecutor 中 的 as_completed() 就是这样一个方法,当子线程中的任务执行完后,直接用 result() 获取返回结果
参考:
python threadpoolexecutor_python 线程池 ThreadPoolExecutor 的用法相关推荐
- python threadpoolexecutor_Python线程池ThreadPoolExecutor实践
Python线程池ThreadPoolExecutor实践 2019年5月15日 23:00 by wst python高级 在使用ThreadPoolExecutor的过程中,一直想探索线程池会使用 ...
- python threadpoolexecutor_python线程池ThreadPoolExecutor用法
线程池,进程池 python的多线程并不是完全鸡肋的存在,得分情况来看.在IO密集型任务下,能提高多倍效率.在CPU密集型任务下,使用多进程也能规避GIL锁. python3标准库concurrent ...
- python线程池wait_python线程池 ThreadPoolExecutor 的用法示例
前言 从Python3.2开始,标准库为我们提供了 concurrent.futures 模块,它提供了 ThreadPoolExecutor (线程池)和ProcessPoolExecutor (进 ...
- python 线程池用法_python 线程池 ThreadPoolExecutor 的用法
1. 线程池的基本用法 # coding: utf-8 from concurrent.futures import ThreadPoolExecutor import time def spider ...
- Python运用线程池ThreadPoolExecutor多线程下载搜索图片
用chrome解析某搜索网站图片搜索请求包,再参考网上一些已有的代码,在vscode+python3.7环境,多线程下载搜索结果页面上图片的代码,其中加入了键盘中止和ThreadPoolExecuto ...
- Python 线程池 ThreadPoolExecutor(二) - Python零基础入门教程
目录 一.Python 线程池前言 二.Python 线程池 ThreadPoolExecutor 常用函数 1.线程池 as_completed 函数使用 2.线程池 map 函数使用 3.线程池 ...
- Python 线程池 ThreadPoolExecutor(一) - Python零基础入门教程
目录 一.Python 线程池前言 二.Python 线程池原理 三.Python 线程池 ThreadPoolExecutor 函数介绍 四.Python 线程池 ThreadPoolExecuto ...
- 线程池ThreadPoolExecutor使用简介
2019独角兽企业重金招聘Python工程师标准>>> 一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: Th ...
- 并发编程(七)好用的线程池ThreadPoolExecutor
并发编程专栏系列博客 并发编程(一)python并发编程简介 并发编程(二)怎样选择多线程多进程和多协程 并发编程(三)Python编程慢的罪魁祸首.全局解释器锁GIL 并发编程(四)如何使用多线程, ...
最新文章
- Introdution to 3D Game Programming With DirectX11 第11章 习题解答
- 后缀数组(SA)备忘
- c语言编译器怎么用scanfkl,C语言一些笔记
- 计算机算法知识总结,移动笔试知识点之--计算机类-数据结构与算法知识点总结.pdf...
- c语言中不带任何修饰符的浮点变量,江苏省计算机等级考试二级C语言笔试辅导题目...
- xpath的基本使用
- kafka消费者源码解析
- Java集合系列---红黑树(基于HashMap 超详细!!!)
- 3198元起!vivo X27/X27 Pro发布 升降摄像头+4800万三摄
- Java之StringUtils的常用方法(非常不错)
- mac地址查 计算机名字,怎么看mac地址-教你通过MAC地址查询设备的厂商名称
- 网页内容变化实时监控提醒(多个复杂的监控条件)
- 幼儿-综合素质【2】
- 华为鸿蒙系统是安卓套壳?直接看代码
- 伦敦国王学院计算机申请要求,2020年伦敦国王学院高级计算机专业研究生申请条件及世界排名|学费介绍...
- “临沂公众号推广平台”的操作流程是怎样的?
- 程序员新手上路第一步
- oracle常用函数number,Oracle 常用函数介绍
- 思科无线AP配置之一(使用超级终端连接设备篇)
- ONLYOFFICE与O2OA参加西部教育博览会圆满成功