以后 主要,还是多用 gevent。稳定,持续,最重要的是:速度快。
个人感觉是比 自带的aiohttp + asyncio 要好用,且快。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
'''
gevent downlaod
gevent is much faster than multiprocessing.dummy
'''
import requests
import re
import time
import os
import gevent
from gevent import monkey
monkey.patch_all()
def get_page(url):
cont = requests.get(url).content
with open(url.split('/')[-1], 'wb') as f:
f.write(cont)
urls = [u.strip() for u in open('us.txt', 'r')]
tasks = [gevent.spawn(get_page, url) for url in urls]
gevent.joinall(tasks)