首页 > 其他 > 详细

协程+IO切换实现并发

时间:2019-02-04 12:07:55      阅读:257      评论:0      收藏:0      [点我收藏+]
from gevent import monkey
# 以后代码中遇到IO都会自动执行greenlet的switch进行切换
monkey.patch_all() 
import requests
import gevent


def get_page1(url):
    ret = requests.get(url)
    print(url,ret.content)

def get_page2(url):
    ret = requests.get(url)
    print(url,ret.content)

def get_page3(url):
    ret = requests.get(url)
    print(url,ret.content)

gevent.joinall([
    gevent.spawn(get_page1, 'https://www.python.org/'), # 协程1
    gevent.spawn(get_page2, 'https://www.yahoo.com/'),  # 协程2
    gevent.spawn(get_page3, 'https://github.com/'),     # 协程3
])

协程+IO切换实现并发

原文:https://www.cnblogs.com/apollo1616/p/10351577.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!