• 进程池与线程池


    进程池与线程池 ,开线程池和进程池的方式一模一样

    from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor

    异步方式提交,进程地一个活干完后接着干第二个活,进程只有8个

    from concurrent.futures import ProcessPoolExecutor
    import os,time,random
    def task(name):
        print("name:%s pid:%s is run" % (name,os.getpid()))
        time.sleep(random.randrange(5,10))
    
    if __name__=="__main__":
        pool=ProcessPoolExecutor(8)#default value is os.cpu_count()
        for i in range(10):
            pool.submit(task,"egon%s" % i)#asynchronous
        print("Main")

    Main
    name:egon0 pid:4818 is run
    name:egon1 pid:4819 is run
    name:egon2 pid:4817 is run
    name:egon3 pid:4821 is run
    name:egon4 pid:4822 is run
    name:egon5 pid:4820 is run
    name:egon6 pid:4823 is run
    name:egon7 pid:4824 is run

    name:egon8 pid:4824 is run
    name:egon9 pid:4819 is run

    主线程等待线程结束,shutdown方法计时器实现,走一个进程减1,直到0则结束

    from concurrent.futures import ProcessPoolExecutor
    import os,time,random
    def task(name):
        print("name:%s pid:%s is run" % (name,os.getpid()))
        time.sleep(random.randrange(5,10))
    
    if __name__=="__main__":
        pool=ProcessPoolExecutor(2)#default value is os.cpu_count()
        for i in range(5):
            pool.submit(task,"egon%s" % i)#asynchronous
        pool.shutdown()#close pool,wait process done.
        print("Main")

    name:egon0 pid:4932 is run
    name:egon1 pid:4933 is run

    name:egon2 pid:4932 is run
    name:egon3 pid:4933 is run

    name:egon4 pid:4932 is run

    Main

    线程池使用方法

    from concurrent.futures import ThreadPoolExecutor
    from threading import currentThread
    import os,time,random
    def task():
        print("name:%s pid:%s is run" % (currentThread().getName(),os.getpid()))
        time.sleep(random.randrange(5,10))
    
    if __name__=="__main__":
        pool=ThreadPoolExecutor(2)#default value is os.cpu_count()
        for i in range(5):
            pool.submit(task,)#asynchronous
        pool.shutdown()#close pool,wait process done.
        print("Main")

    name:ThreadPoolExecutor-0_0 pid:5007 is run
    name:ThreadPoolExecutor-0_1 pid:5007 is run

    name:ThreadPoolExecutor-0_1 pid:5007 is run
    name:ThreadPoolExecutor-0_0 pid:5007 is run

    name:ThreadPoolExecutor-0_1 pid:5007 is run
    Main

  • 相关阅读:
    Codeforces Round #226 (Div. 2)
    内存管理
    C/C++ 函数
    Codeforces Round #225 (Div. 2)
    常用链表操作总结
    Codeforces Round #224 (Div. 2)
    Codeforces Round #223 (Div. 2)
    Codeforces Round #222 (Div. 2)
    -树-专题
    Codeforces Round #221 (Div. 2)
  • 原文地址:https://www.cnblogs.com/yaya625202/p/9048225.html
Copyright © 2020-2023  润新知