zoukankan      html  css  js  c++  java
  • 20.multi_case01

    # 多进程,使用Pool
    from multiprocessing import Pool
    
    def f(x):
        return x*x
    
    if __name__ == '__main__':
        p = Pool(5)
        list = [1,2,3,4,5,6,7,8,9]
        print(p.map(f, list))
    
    # 多进程,使用Pool
    import time
    import requests
    from multiprocessing import Pool
    
    task_list = [
        'https://www.jianshu.com/p/91b702f4f24a',
        'https://www.jianshu.com/p/8e9e0b1b3a11',
        'https://www.jianshu.com/p/7ef0f606c10b',
        'https://www.jianshu.com/p/b117993f5008',
        'https://www.jianshu.com/p/583d83f1ff81',
        'https://www.jianshu.com/p/91b702f4f24a',
        'https://www.jianshu.com/p/8e9e0b1b3a11',
        'https://www.jianshu.com/p/7ef0f606c10b',
        'https://www.jianshu.com/p/b117993f5008',
        'https://www.jianshu.com/p/583d83f1ff81'
    ]
    
    header = {
        'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 '
        '(KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
    
    
    def download(url):
        response = requests.get(url,
                                headers=header,
                                timeout=30
                                )
        return response.status_code
    
    
    if __name__ == '__main__':
        p = Pool(10)
        time_old = time.time()
        for item in p.map(download, task_list):
            print(item)
        time_new = time.time()
        time_cost = time_new - time_old
        print(time_cost)
    
  • 相关阅读:
    【.NET】Web Service
    【Coding】C# 操作文件(一)
    【设计模式】设计模式概述
    TCP/IP协议
    【.NET】SOAP Web Service
    简单读写xml
    利用winform来承载WCF服务
    在panel里面显示一个窗体
    asp.net 角色管理 MSDN帮助路径
    asp.net ajax MSDN帮助
  • 原文地址:https://www.cnblogs.com/hankleo/p/10820112.html
Copyright © 2011-2022 走看看