• day 32-2 练习


    基于 requests 实现简单线程池的运用

    from concurrent.futures import ThreadPoolExecutor
    import requests
    
    
    def gets(url):
        print('正在获取 %s' % url)
        info = requests.get(url)
        num = len(info.text)
        return {'url': url, 'num': num}
    
    
    def ret(info):
        info = info.result()
        print('%s 的长度为 %s' % (info['url'], info['num']))
    
    
    if __name__ == '__main__':
        url = ['https://www.cnblogs.com/ysging/p/12339912.html',
               'https://www.cnblogs.com/ysging/p/12329308.html',
               'https://www.cnblogs.com/ysging/p/12319763.html']
        t = ThreadPoolExecutor(2)
        for i in url:
            t.submit(gets, i).add_done_callback(ret)
    
    # 执行结果
    # 正在获取 https://www.cnblogs.com/ysging/p/12339912.html
    # 正在获取 https://www.cnblogs.com/ysging/p/12329308.html
    # https://www.cnblogs.com/ysging/p/12329308.html 的长度为 41115
    # 正在获取 https://www.cnblogs.com/ysging/p/12319763.html
    # https://www.cnblogs.com/ysging/p/12339912.html 的长度为 53267
    # https://www.cnblogs.com/ysging/p/12319763.html 的长度为 30937
  • 相关阅读:
    Redis主从复制
    Redis发布订阅
    Redis持久化
    初探redis.config
    java连接Linux服务器问题
    Redis常见类型及API
    Redis安装
    Nosql
    JMM
    SpringSecurity
  • 原文地址:https://www.cnblogs.com/ysging/p/12348661.html
Copyright © 2020-2023  润新知