线程池

一、在python3中开多线程

使用concurrent.futures模块,这个模块是python3中自带的模块

  • 1、开个线程池的代码

    import time
    from concurrent.futures import ThreadPoolExecutor
    
    def get_html(times):
        time.sleep(times)
        print('获取页面数据:{}'.format(times))
        return times
    
    if __name__ == "__main__":
        # 最大可以容许2个线程进程
        executor = ThreadPoolExecutor(max_workers=2)
        task1 = executor.submit(get_html, 3)
        task2 = executor.submit(get_html, 2)
    
  • 2、一些方法介绍

    • 1.submit()向线程池中提交一个方法
    • 2.done()查看结果
    • 3.cancel()取消(只有在没启动的时候才能取消)
    • 4.result()获取线程池中执行的结果
  • 3、使用as_completed批量提交任务(无顺序的执行)

    from concurrent.futures import ThreadPoolExecutor, as_completed, wait
    import time
    
    def get_html(times):
        time.sleep(times)
        print('获取页面数据:{}'.format(times))
        return times
    
    if __name__ == "__main__":
        urls_list = [4, 2, 1, 3, 6]
        executor = ThreadPoolExecutor(max_workers=2)
        all_task = [executor.submit(get_html, (url)) for url in urls_list]
        for item in as_completed(all_task):
            data = item.result()
            print('main==>', data)
    
  • 4、使用map批量提交任务(有顺序的执行)

    from concurrent.futures import ThreadPoolExecutor, as_completed, wait
    import time
    
    def get_html(times):
        time.sleep(times)
        print('获取页面数据:{}'.format(times))
        return times
    
    if __name__ == "__main__":
        urls_list = [4, 2, 1, 3, 6]
        executor = ThreadPoolExecutor(max_workers=2)
    
        for item in executor.map(get_html, urls_list):
            print('main==>', item)
    
        print('结束程序')
    
  • 5、wait()等待什么执行完后进行下面的

results matching ""

    No results matching ""