守护进程
主进程创建守护进程
其一:守护进程会在主代码执行结束后终止
其二:守护进程无法在其内部开启子进程,否则报错
注:进程之间的运行是相互独立的,主代码执行完,守护进程随即结束
from multiprocessing import Process
import time
import random
class Piao(Process):
def __init__(self,name):
super().__init__()
self.name=name
def run(self):
print('%s is piaoing'%self.name)
time.sleep(random.randrange(1,3))
print('%s is piao end'%self.name)
if __name__ == '__main__':
p=Piao('egon')
p.daemon=True#一定要在p.start()前设置,设置p为守护进程,禁止p创建子进程,并且父进程代码执行结束,p即终止运行
p.start()
print('主')
#输出结果:主
from multiprocessing import Process
import time
import random
def foo(name):
print('%s is piaoing'%name)
time.sleep(random.randrange(1,3))
print('%s is piao end'%name)
def bar(name):
print('%s is piaoing'%name)
time.sleep(random.randrange(1,3))
print('%s is piao end&