zoukankan      html  css  js  c++  java
  • python concurrent 的使用测试

    说明

    Python3.2开始,标准库为我们提供了concurrent.futures模块,它提供了ThreadPoolExecutor和ProcessPoolExecutor两个类,实现了对threading和multiprocessing的进一步抽象,对编写线程池/进程池提供了直接的支持.

    代码测试

    #! /usr/bin/env python
    # -*- coding: utf-8 -*-#
    
    # -------------------------------------------------------------------------------
    # Name:         demo3
    # Author:       yunhgu
    # Date:         2021/7/8 15:17
    # Description: 
    # -------------------------------------------------------------------------------
    import os
    import time
    import threading
    from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed
    
    
    def work(x):
        time.sleep(1)
        temp = f"父进程{os.getppid()}:子进程{os.getpid()}:线程{threading.get_ident()}:{x}"
        return temp
    
    
    def sub_thread():
        temp_list = []
        with ThreadPoolExecutor(max_workers=3) as t:
            task_list = [t.submit(work, i) for i in range(5)]
            for task in as_completed(task_list):
                if task.done():
                    temp_list.append(task.result())
        return temp_list
    
    
    def main():
        print(f"主进程:{os.getpid()}")
        path_list = []
        with ProcessPoolExecutor(max_workers=3) as p:
            task_list = [p.submit(sub_thread) for i in range(5)]
            for task in as_completed(task_list):
                if task.done():
                    path_list.append(task.result())
        for path in path_list:
            print(path)
    
    
    if __name__ == '__main__':
        main()
    

    image

    不论你在什么时候开始,重要的是开始之后就不要停止。 不论你在什么时候结束,重要的是结束之后就不要悔恨。
  • 相关阅读:
    iptables操作指令
    最怕问初衷,大梦成空
    kettle官网wiki
    Pentaho Spoon (Kettle) 出现 Timestamp :Unable to get timestamp from resultset at index 30 错误的解决
    win10安装vs2013失败解决方法
    2020 最新 kail 安装教程
    01_docker学习笔记(一)
    数据结构的基础概念
    BOS3
    配置文件添加druid控制台
  • 原文地址:https://www.cnblogs.com/yunhgu/p/15005535.html
Copyright © 2011-2022 走看看