• 守护进程

    (1)守护进程在主进程代码运行结束后,就自动终止

    (2)守护进程内无法开启子进程,否则报错AssertionError: daemonic processes are not allowed to have children

    注意:进程之间相互独立的,主进程运行完毕后,守护进程随即结束

from multiprocessing import Process
import os,time, random
def func():
    print('%s ruing '%os.getpid())
    time.sleep(0.2)
    print('%s end'%os.getpid())
    # p = Process(target=func) #在子进程内创建以个子进程,报错
    # p.start()

if __name__ == '__main__':
    P = Process(target=func)
    P.daemon = True
    P.start()
    # P.join()# 使主进程必须等到子进程运行完毕
    print('')

 

from multiprocessing import Process
from threading import Thread
import time
def foo():
    print(123)
    time.sleep(1)
    print("end123")

def bar():
    print(456)
    time.sleep(3)
    print("end456")

if __name__ == '__main__':
    p1=Process(target=foo)
    p2 = Process(target=bar)

    p1.daemon=True
    p1.start()
    p2.start()
    print("main-------")#打印出“main”意味着主进程结束,守护进程也就结束了,然而p2并不是守护进行,主进程需等到p2运行完毕,才能结束,否则会产生孤儿进程
迷惑人的例子

相关文章:

  • 2021-05-24
  • 2022-01-04
  • 2021-11-19
  • 2021-10-27
  • 2021-12-17
  • 2022-01-26
  • 2021-06-06
  • 2021-08-16
猜你喜欢
  • 2021-12-02
  • 2022-12-23
  • 2022-01-16
  • 2021-04-16
  • 2021-07-07
相关资源
相似解决方案