• 进程池与线程池


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

    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

  • 相关阅读:
    MSSQL慢查询查询与统计
    SQLServer统计采集数据库相关信息
    python3 安装pymssql失败 pip3 install pymssql
    CentOS7源码安装Python3
    python3 安装pyodbc失败 pip3 install pyodbc
    Django配置Mysql数据库 (Pycharm)
    DBArtist之Oracle入门第4步: Oracle创建数据库
    DBArtist之Oracle入门第3步: 安装配置PL/SQL Developer
    linux三剑客
    python + SMTP 发送邮件
  • 原文地址:https://www.cnblogs.com/yaya625202/p/9048225.html
Copyright © 2020-2023  润新知