zoukankan      html  css  js  c++  java
  • python 同步与异步的性能区别以及遇到IO阻塞时会自动切换任务


    #网页爬取
    import urllib,time,gevent
    import urllib.request
    from gevent import monkey

    monkey.patch_all()#把当前程序的所有IO的操作给我单独的做上标记

    def f(url):
    print('GET: %s' % url)
    resp = urllib.request.urlopen(url)
    data = resp.read()
    print('%d bytes received from %s.' % (len(data), url))

    time_start = time.time()
    url_i =['https://www.python.org/',
    'https://github.com/',
    'https://www.jenkins.io/']
    for url in url_i:
    f(url)
    print('同步耗时:',time.time()-time_start)

    now_time = time.time()
    gevent.joinall([
    gevent.spawn(f,'https://www.python.org/'),
    gevent.spawn(f,'https://github.com/'),
    gevent.spawn(f,'https://www.jenkins.io/'),
    ])
    print('异步耗时:',time.time()-now_time)
  • 相关阅读:
    edu_6_1_4
    edu_6_1_2
    edu_6_1_3
    edu_6_1_1
    音乐链接
    音乐推荐界面
    客服页面
    购物页面
    京东读书新闻资讯页面
    安装Tomcat时 ,设置JAVA_HOME和JRE_HOME
  • 原文地址:https://www.cnblogs.com/anhao-world/p/13748375.html
Copyright © 2011-2022 走看看