码迷,mamicode.com
首页 > 其他好文 > 详细

协程框架

时间:2020-01-01 23:38:34      阅读:99      评论:0      收藏:0      [点我收藏+]

标签:baidu   int   win   text   key   start   print   app   user   

import requests
import gevent 
from gevent import monkey

monkey.patch_all()


headers = {
  'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
  'Accept-Language': 'en',
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36'
}

def download(url):
    print("start:",url)
    content = requests.get(url, headers=headers).content
    gevent.sleep(1)
    print(len(content), url)
    pass    

def main():
    gevent.joinall([
        gevent.spawn(download, "http://www.baidu.com"),    
        gevent.spawn(download, "http://www.bilibili.com"),    
        gevent.spawn(download, "http://www.163.com"),    
        gevent.spawn(download, "http://www.sina.com"),   
    ])
    
    
if __name__ == "__main__":
    main()

协程框架

标签:baidu   int   win   text   key   start   print   app   user   

原文地址:https://www.cnblogs.com/zxfei/p/12130889.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!