zoukankan      html  css  js  c++  java
  • 进程池与线程池

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

    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

  • 相关阅读:
    字符替换
    并发编程之线程创建
    禁止浏览器缓存文件
    Java垃圾回收机
    带你走进ajax
    MFC的CString使用
    《Java小游戏》:球球大作战
    【推荐】开源项目ElasticAmbari助力 ElasticSearch、Kibana、ambari服务高效运维管理
    Java基础
    什么是可串行化MVCC
  • 原文地址:https://www.cnblogs.com/yaya625202/p/9048225.html
Copyright © 2011-2022 走看看