zoukankan      html  css  js  c++  java
  • asyncio结合线程池

    #使用多线程:在协程中集成阻塞io
    import asyncio
    from concurrent.futures import ThreadPoolExecutor
    import socket
    from urllib.parse import urlparse
    
    
    def get_url(url):
        #通过socket请求html
        url = urlparse(url)
        host = url.netloc
        path = url.path
        if path == "":
            path = "/"
    
        #建立socket连接
        client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        # client.setblocking(False)
        client.connect((host, 80)) #阻塞不会消耗cpu
    
        #不停的询问连接是否建立好, 需要while循环不停的去检查状态
        #做计算任务或者再次发起其他的连接请求
    
        client.send("GET {} HTTP/1.1
    Host:{}
    Connection:close
    
    ".format(path, host).encode("utf8"))
    
        data = b""
        while True:
            d = client.recv(1024)
            if d:
                data += d
            else:
                break
    
        data = data.decode("utf8")
        html_data = data.split("
    
    ")[1]
        print(html_data)
        client.close()
    
    
    if __name__ == "__main__":
        import time
        start_time = time.time()
        loop = asyncio.get_event_loop()
        executor = ThreadPoolExecutor(3)
        tasks = []
        for url in range(20):
            url = "http://shop.projectsedu.com/goods/{}/".format(url)
            task = loop.run_in_executor(executor, get_url, url)
            tasks.append(task)
        loop.run_until_complete(asyncio.wait(tasks))
        print("last time:{}".format(time.time()-start_time))
  • 相关阅读:
    kubernetes-harbor 私有仓库 帐号与密码 配置
    cloudstack4.11 centos7 安装文档
    检查hdfs块的块-fsck
    Kubernetes 1.13 正式发布,功能亮点一览!
    SnakeYaml快速入门
    chromedriver和ffdriver下载地址
    Python 卸载
    Apache httpclient
    Selenium文件上传
    Java Runtime.exec()用法
  • 原文地址:https://www.cnblogs.com/Erick-L/p/8933990.html
Copyright © 2011-2022 走看看