python3 gevent模块(遇到IO自动切换)

# -*- 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

 

使用gevent模块爬取网页

# -*- 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

 

转载于:https://www.cnblogs.com/lilyxiaoyy/p/11037401.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值