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

Posted lilyxiaoyy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

 

以上是关于python3 gevent模块(遇到IO自动切换)的主要内容,如果未能解决你的问题,请参考以下文章

简述 gevent模块的作用和应用场景。

多协程间遇到IO自动切换gevent

python-gevent模块(自动切换io的协程)

gevent

gevent 实现io自动切换,gevent.join([]), gevent.spawn, 爬虫多并发的实现

gevent异步,io自动切换