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()
网友评论