zoukankan      html  css  js  c++  java
  • 网络编程

    一、协议遇到IO自动切换

    python网络编程,遇到IO自动切换,通过模块gevent来实现;

    import gevent,time

    def g1():
    print ("g1 is start!")
    gevent.sleep(3)
    print ("g1 is end!")
    def g2():
    print ("g2 is start!")
    gevent.sleep(2)
    print ("g2 is end!")
    def g3():
    print ("g3 is start!")
    gevent.sleep(1)
    print ("g3 is end!")

    gevent.joinall([gevent.spawn(g1),
    gevent.spawn(g2),
    gevent.spawn(g3)])
    二、协议gevent并发爬网页
    import gevent,time
    from urllib import request
    from gevent import monkey
    monkey.patch_all()#把当前io的所有操作做上标记(如果未导入该语句,gevent无法确认urllib是io操作);

    def f(url):
    print ("GET %s" % url)
    res =request.urlopen(url)
    data = res.read()
    # f= open("a.html","wb")
    # f.write(data)
    # f.close()
    print ("%d bytes received from %s" %(len(data),url))
    urls = ["https://www.python.org/",
    "https://www.yahoo.com/",
    "https://www.github.com/"]
    time_start = time.time()
    for url in urls:
    f(url)
    total_time = time.time()-time_start
    print (total_time)
    time_start1 = time.time()
    gevent.joinall([
    gevent.spawn(f,"https://www.python.org/"),
    gevent.spawn(f,"https://www.yahoo.com/"),
    gevent.spawn(f,"https://www.github.com/")
    ])
    async_time = time.time() - time_start1
    print (async_time)
    运行结果:

    GET https://www.python.org/
    48990 bytes received from https://www.python.org/
    GET https://www.yahoo.com/
    508828 bytes received from https://www.yahoo.com/
    GET https://www.github.com/
    79979 bytes received from https://www.github.com/
    5.865999937057495
    GET https://www.python.org/
    GET https://www.yahoo.com/
    GET https://www.github.com/
    508974 bytes received from https://www.yahoo.com/
    48990 bytes received from https://www.python.org/
    79979 bytes received from https://www.github.com/
    1.371999979019165

    
    

    Process finished with exit code 0

    从结果中,可以看出,for循环,默认为串行操作;gevent.joinall操作,默认遇到IO操作时,自动切换成并行;
  • 相关阅读:
    【codeforces 797C】Minimal string
    【codeforces 797E】Array Queries
    【codeforces 796C】Bank Hacking(用一些技巧来代替multiset)
    【POJ 1860】Currency Exchange
    【微软2017年预科生计划在线编程笔试 A】Legendary Items
    【微软2017年预科生计划在线编程笔试 B】Tree Restoration
    【codeforces 797D】Broken BST
    【[Offer收割]编程练习赛11 C】岛屿3
    【[Offer收割]编程练习赛11 B】物品价值
    【codeforces 789D】Weird journey
  • 原文地址:https://www.cnblogs.com/wulafuer/p/10281910.html
Copyright © 2011-2022 走看看