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

    不论你在什么时候开始,重要的是开始之后就不要停止。 不论你在什么时候结束,重要的是结束之后就不要悔恨。
  • 相关阅读:
    面试常问题
    雷达无线电系列(五)拟合优度检验(matlab)
    LDAP轻量级目录访问协议总结(待完成)
    zipkin数据追踪(待完成)
    git多定制版本管理
    git单修改推送多分支(cherry-pick)
    git工作流及插件使用说明
    基金-股票-期权知识总结(待完成)
    java AESRSA加密解密样例
    分享一个统计文档中不同key的个数的python脚本
  • 原文地址:https://www.cnblogs.com/yunhgu/p/15005535.html
Copyright © 2011-2022 走看看