多进程续集
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多进程续集相关的知识,希望对你有一定的参考价值。
1.守护进程"守护进程的特点"
1.守护进程会在主进程的代码执行完成后终止
2.守护进程内不能再开启子进程,否则抛出异常AssertionError: daemonic processes are not allowed to have children
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: vita
from multiprocessing import Process
import time
import random
def task(name):
print(‘%s is piaoing‘ %name)
time.sleep(random.randrange(1,3))
print(‘%s is piao end‘ %name)
if __name__ == ‘__main__‘:
p=Process(target=task,args=(‘egon‘,))
p.daemon=True #一定要在p.start()前设置,设置p为守护进程,禁止p创建子进程,并且父进程代码执行结束,p即终止运行
p.start()
print(‘主‘) #只要终端打印出这一行内容,那么守护进程p也就跟着结束掉了
D:\software2\Python3\install\python.exe E:/PythonProject/new-python/python-test/BasicGrammer/test.py
主
Process finished with exit code 0
from multiprocessing import Process
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-------") #主进程执行玩这个,p1就停止了
D:\software2\Python3\install\python.exe E:/PythonProject/new-python/python-test/BasicGrammer/test.py
main-------
456
end456
Process finished with exit code 0
以上是关于多进程续集的主要内容,如果未能解决你的问题,请参考以下文章