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

  • 相关阅读:
    html中#include file的使用方法
    【leetcode】Next Permutation
    diameter
    android listview综合使用演示样例_结合数据库操作和listitem单击长按等事件处理
    【剑指offer】链表倒数第k个节点
    【经典面试题】实现平方根函数sqrt
    对CAB文件进行数字签名
    实习面试总结
    从队列、二叉树到优先队列
    句子的理解
  • 原文地址:https://www.cnblogs.com/yaya625202/p/9048225.html
Copyright © 2011-2022 走看看