码迷,mamicode.com
首页 > 系统相关 > 详细

多进程续集

时间:2019-05-20 13:25:16      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:sof   内容   time   from   子进程   rgs   rom   mon   bin   

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

多进程续集

标签:sof   内容   time   from   子进程   rgs   rom   mon   bin   

原文地址:https://blog.51cto.com/10983441/2397249

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!