本文主要介绍 Python threading 单线程与多线程爬虫实战速度测试对比
Python Threading 多线程爬虫实战-cnblogs.py
import requests
headers = {
"user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36"
}
urls = [
f"https://www.cnblogs.com/#p{page}"
for page in range(1,50+1)
]
def craw(url):
r = requests.get(url, headers=headers)
print(url, len(r.text))
return r.text
Python Threading 多线程爬虫实战 单线程 single_thread.py
import cnblogs
import time
# 单线程
def single_thread():
print("single_thread begin")
for url in cnblogs.urls:
cnblogs.craw(url)
print("single_thread end")
if __name__ == "__main__":
start = time.time()
single_thread()
end = time.time()
print("single thread const:", end - start, 'seconds')
Python Threading 多线程爬虫实战-多线程 multi_thread.py
import cnblogs
import threading
import time
# 多线程
def multi_thread():
print("multi_thread begin")
threads = []
# 创建线程
for url in cnblogs.urls:
threads.append(
threading.Thread(target=cnblogs.craw, args=(url,))
)
# 启动线程
for thread in threads:
thread.start()
# 等待线程结束
for thread in threads:
thread.join()
print("multi_thread end")
if __name__ == "__main__":
start = time.time()
multi_thread()
end = time.time()
print("multi thread const:", end - start, 'seconds')
Python Threading 多线程爬虫实战-单线程测试结果
单线程测试用时 single thread const: 11.669063806533813 seconds
(venv) fujie@fujuhaos-MacBook-Pro threading % python single_thread.py
single_thread begin
https://www.cnblogs.com/#p1 69472
https://www.cnblogs.com/#p2 69472
https://www.cnblogs.com/#p3 69472
https://www.cnblogs.com/#p4 69472
https://www.cnblogs.com/#p5 69472
https://www.cnblogs.com/#p6 69472
https://www.cnblogs.com/#p7 69472
https://www.cnblogs.com/#p8 69472
https://www.cnblogs.com/#p9 69472
https://www.cnblogs.com/#p10 69472
https://www.cnblogs.com/#p11 69472
https://www.cnblogs.com/#p12 69472
https://www.cnblogs.com/#p13 69472
https://www.cnblogs.com/#p14 69472
https://www.cnblogs.com/#p15 69472
https://www.cnblogs.com/#p16 69472
https://www.cnblogs.com/#p17 69472
https://www.cnblogs.com/#p18 69472
https://www.cnblogs.com/#p19 69472
https://www.cnblogs.com/#p20 69472
https://www.cnblogs.com/#p21 69472
https://www.cnblogs.com/#p22 69472
https://www.cnblogs.com/#p23 69472
https://www.cnblogs.com/#p24 69472
https://www.cnblogs.com/#p25 69472
https://www.cnblogs.com/#p26 69472
https://www.cnblogs.com/#p27 69472
https://www.cnblogs.com/#p28 69472
https://www.cnblogs.com/#p29 69472
https://www.cnblogs.com/#p30 69472
https://www.cnblogs.com/#p31 69472
https://www.cnblogs.com/#p32 69472
https://www.cnblogs.com/#p33 69472
https://www.cnblogs.com/#p34 69472
https://www.cnblogs.com/#p35 69472
https://www.cnblogs.com/#p36 69472
https://www.cnblogs.com/#p37 69472
https://www.cnblogs.com/#p38 69472
https://www.cnblogs.com/#p39 69472
https://www.cnblogs.com/#p40 69472
https://www.cnblogs.com/#p41 69472
https://www.cnblogs.com/#p42 69472
https://www.cnblogs.com/#p43 69472
https://www.cnblogs.com/#p44 69472
https://www.cnblogs.com/#p45 69472
https://www.cnblogs.com/#p46 69472
https://www.cnblogs.com/#p47 69472
https://www.cnblogs.com/#p48 69472
https://www.cnblogs.com/#p49 69472
https://www.cnblogs.com/#p50 69472
single_thread end
single thread const: 11.669063806533813 seconds
Python Threading 多线程爬虫实战-多线程测试结果
多线程测试用时: multi thread const: 1.8338000774383545 seconds
(venv) fujie@fujuhaos-MacBook-Pro threading % python multi_thread.py
multi_thread begin
https://www.cnblogs.com/#p9 69472
https://www.cnblogs.com/#p6 69472
https://www.cnblogs.com/#p10 69472
https://www.cnblogs.com/#p3 69472
https://www.cnblogs.com/#p1 69472
https://www.cnblogs.com/#p7 69472
https://www.cnblogs.com/#p15 69472
https://www.cnblogs.com/#p8 69472
https://www.cnblogs.com/#p2 69472
https://www.cnblogs.com/#p18 69472
https://www.cnblogs.com/#p22 69472
https://www.cnblogs.com/#p4 69472
https://www.cnblogs.com/#p11 69472
https://www.cnblogs.com/#p26 69472
https://www.cnblogs.com/#p16 69472
https://www.cnblogs.com/#p24 69472
https://www.cnblogs.com/#p30 69472
https://www.cnblogs.com/#p19 69472
https://www.cnblogs.com/#p17 69472
https://www.cnblogs.com/#p12 69472
https://www.cnblogs.com/#p21 69472
https://www.cnblogs.com/#p20 69472
https://www.cnblogs.com/#p47 69472
https://www.cnblogs.com/#p43 69472
https://www.cnblogs.com/#p37 69472
https://www.cnblogs.com/#p31 69472
https://www.cnblogs.com/#p25 69472
https://www.cnblogs.com/#p14 69472
https://www.cnblogs.com/#p5 69472
https://www.cnblogs.com/#p29 69472
https://www.cnblogs.com/#p45 69472
https://www.cnblogs.com/#p13 69472
https://www.cnblogs.com/#p48 69472
https://www.cnblogs.com/#p39 69472
https://www.cnblogs.com/#p41 69472
https://www.cnblogs.com/#p44 69472
https://www.cnblogs.com/#p49 69472
https://www.cnblogs.com/#p34 69472
https://www.cnblogs.com/#p23 69472
https://www.cnblogs.com/#p40 69472
https://www.cnblogs.com/#p46 69472
https://www.cnblogs.com/#p42 69472
https://www.cnblogs.com/#p32 69472
https://www.cnblogs.com/#p27 69472
https://www.cnblogs.com/#p33 69472
https://www.cnblogs.com/#p28 69472
https://www.cnblogs.com/#p50 69472
https://www.cnblogs.com/#p36 69472
https://www.cnblogs.com/#p35 69472
https://www.cnblogs.com/#p38 69472
multi_thread end
multi thread const: 1.8338000774383545 seconds
以上就是Python单线程与多线程测试对比,使用Python threading多线程效果还是比较明显的!
The Posts
- Python 并发编程实战 使用队列(Queue)实现生产者、消费者多线程爬虫Jul 7, 2022
- Python threading单线程与多线程爬虫实战速度测试对比Jul 4, 2022
- Python flask web项目使用线程池ThreadPoolExecutor并发执行实战Jul 4, 2022
- Laravel 9 基于自定义令牌的 Api 身份验证教程May 14, 2022
- 使用Laravel9的Sanctum REST API 身份验证示例May 14, 2022
- Laravel9的HttpBasicAuth基本认证May 9, 2022
- Markdown与html互转markdown.js_showdown.js_marked.js_turndown.jsApr 5, 2022
- WordPress后台在线更新失败问题解决Apr 4, 2022
- WordPress固定链接伪静态.html结尾Mar 28, 2022
- WordPress主题插件安装下载Mar 26, 2022
- 个人网站搭建流程及免费源码推荐Mar 26, 2022
- ES11新特性_matchAll_私有属性_allSettled_可选链操作符_动态import导入_BigInt_globalThis对象Mar 24, 2022
- ES10新特性_fromEntries_trimStart_trimEnd_flat_flatMap_descriptionMar 24, 2022
- ES9_正则扩展_正则表达式_命名捕获分组_反向断言_dotAll 模式Mar 23, 2022
- ES9_rest参数与es9_spread扩展运算符Mar 23, 2022
- ES8新特性_ECMAScript8_object对象方法扩展Mar 19, 2022
- ES8新特性_ECMAScript8_async_awaitMar 19, 2022
- Sublime Text3 配置Python3 详细教程Mar 19, 2022
- ES7新特性_ECMAScript7_includes_指数运算符Mar 19, 2022
- ES6新特性_模块化(module)Mar 17, 2022