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

    不论你在什么时候开始,重要的是开始之后就不要停止。 不论你在什么时候结束,重要的是结束之后就不要悔恨。
  • 相关阅读:
    三十二、恢复单库单表
    三十一、XBK备份
    三十、分库分表备份脚本
    二十九、mysqldump恢复案例
    二十八、mysqldump备份
    二十七、备份介绍
    二十六:慢日志分析
    二十五、二进制日志之GTID模式
    Trie树
    AC自动机
  • 原文地址:https://www.cnblogs.com/yunhgu/p/15005535.html
Copyright © 2011-2022 走看看