守护进程

守护进程

p.daemon = True

主进程会等待所有的子进程结束,是为了回收子进程的资源

守护进程会等待主进程的代码执行结束之后再结束,而不是等待整个主进程结束.

主进程的代码什么时候结束,守护进程就什么时候结束,和其他子进程的执行进度无关

import time
from multiprocessing import Process

def son1():
    while True:
        print('--> in son1')
        time.sleep(1)

def son2():   # 执行10s
    for i in range(10):
        print('in son2')
        time.sleep(1)

if __name__ == '__main__':    # 3s
    p1 = Process(target=son1)
    p1.daemon = True    # 表示设置p1是一个守护进程
    p1.start()
    p2 = Process(target=son2,)
    p2.start()
    time.sleep(3)
    print('in main')
import time
from multiprocessing import Process

def son1():
    while True:
        print('--> in son1')
        time.sleep(1)

def son2():   # 执行10s
    for i in range(10):
        print('in son2')
        time.sleep(1)

if __name__ == '__main__':    # 3s
    p1 = Process(target=son1)
    p1.daemon = True    # 表示设置p1是一个守护进程
    p1.start()
    p2 = Process(target=son2,)
    p2.start()
    time.sleep(3)
    print('in main')
    p2.join()    # 等待p2结束之后才结束

# 等待p2结束 --> 主进程的代码才结束 --> 守护进程结束
posted @ 2020-07-23 12:45  爱浪的跳蚤  阅读(107)  评论(0编辑  收藏  举报