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

    不论你在什么时候开始,重要的是开始之后就不要停止。 不论你在什么时候结束,重要的是结束之后就不要悔恨。
  • 相关阅读:
    bzoj-2748 2748: [HAOI2012]音量调节(dp)
    bzoj-2338 2338: [HNOI2011]数矩形(计算几何)
    bzoj-3444 3444: 最后的晚餐(组合数学)
    codeforces 709E E. Centroids(树形dp)
    codeforces 709D D. Recover the String(构造)
    codeforces 709C C. Letters Cyclic Shift(贪心)
    codeforces 709B B. Checkpoints(水题)
    codeforces 709A A. Juicer(水题)
    Repeat Number
    hdu 1003 Max Sum (动态规划)
  • 原文地址:https://www.cnblogs.com/yunhgu/p/15005535.html
Copyright © 2011-2022 走看看