美文网首页
python 进程池 进程挂掉自动启动

python 进程池 进程挂掉自动启动

作者: 青铜搬砖工 | 来源:发表于2018-11-23 17:21 被阅读0次
    import time
    from multiprocessing import Pool
    import os
    
    count = 1
    
    def f(index):
    
        while True:
            print(" i am working {},pid:{}".format(index,os.getpid()))
    
            time.sleep(1)
    
    
    def main():
        pool = Pool(processes=2)    # set the processes max number 3
    
    
        while True:
    
            result = pool.apply_async(f, (1,))
            result = pool.apply_async(f, (2,))
            result = pool.apply_async(f, (3,))
            print("等待3s")
            time.sleep(3)
        pool.close()
        pool.join()
    
    
    if __name__ == "__main__":
        main()
    

    进程池的大小为2,所以一开始启动时只能启动两个进程输出如下:


    image.png

    当手动杀掉进程1后,进程池空出一个位置所以进程3可以输出输出如下:


    image.png

    所以当进程池的大小为1时 则就会在进程挂掉后自动重启

    代码如下:

    import time
    from multiprocessing import Pool
    import os
    
    count = 1
    
    def f(index):
    
        while True:
            print(" i am working {},pid:{}".format(index,os.getpid()))
    
            time.sleep(1)
    
    
    def main():
        pool = Pool(processes=1)    # set the processes max number 3
    
    
        while True:
    
            result = pool.apply_async(f, (1,))
    
            print("等待3s")
            time.sleep(3)
        pool.close()
        pool.join()
    
    
    if __name__ == "__main__":
        main()
    

    相关文章

      网友评论

          本文标题:python 进程池 进程挂掉自动启动

          本文链接:https://www.haomeiwen.com/subject/fytsqqtx.html