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)
    
  • 相关阅读:
    logstash Codec
    mysql 插入前 锁表问题
    数据统一管理--企业决策分析之刚需
    数据统一管理--企业决策分析之刚需
    MySQL RR隔离 读一致性
    java HashTable
    mysl lock table read
    mysql DBI 事务控制
    Fiddler模拟http请求
    haproxy hdr_beg 配置
  • 原文地址:https://www.cnblogs.com/hankleo/p/10820112.html
Copyright © 2011-2022 走看看