python3 gevent模块(遇到IO自动切换)
2020-12-13 02:25
标签:yahoo strong cond http github pat 切换 size str 爬取网页 使用gevent模块爬取网页 python3 gevent模块(遇到IO自动切换) 标签:yahoo strong cond http github pat 切换 size str 原文地址:https://www.cnblogs.com/lilyxiaoyy/p/11037401.html# -*- coding: utf-8 -*-
from gevent import monkey;monkey.patch_all() # 记住一定放在第一行,这里是打补丁的意思
import gevent
import time
def eat(name):
print("%s eat first" % name)
time.sleep(3)
print("%s eat second" % name)
def play(name):
print("%s play phone 1" % name)
time.sleep(2)
print("%s play phone 2" % name)
g1 = gevent.spawn(eat, "lily")
g2 = gevent.spawn(play, name="lily")
g1.join()
g2.join()
# lily eat first
# lily play phone 1
# lily play phone 2
# lily eat second
# -*- coding: utf-8 -*-
import time
import requests
def get_page(url):
response = requests.get(url)
print(url)
if response.status_code == 200:
print(response.text)
start_time = time.time()
get_page("https://www.python.org")
get_page("https://www.yahoo.com")
get_page("https://github.com")
print("执行时间:%s" % (time.time()-start_time))
# 执行时间:49.088807821273804
# -*- coding: utf-8 -*-
from gevent import monkey;monkey.patch_all()
import gevent
import time
import requests
def get_page(url):
response = requests.get(url)
print(url)
if response.status_code == 200:
print(response.text)
start_time = time.time()
g1 = gevent.spawn(get_page, "https://www.python.org")
g2 = gevent.spawn(get_page, "https://www.yahoo.com")
g3 = gevent.spawn(get_page, "https://github.com")
gevent.joinall([g1, g2, g3])
print("执行时间:%s" % (time.time()-start_time))
# 执行时间:29.85470747947693
下一篇:JS中使用EL表达式