python_多线程中的一点问题

本文核心:

python中所有的子线程是否是守护进程都继承自主线程,因为主线程默认是非守护进程,因此,所有的由该主线程创建的子线程都不是守护进程。

当所有的非守护进程结束的时候,python程序也就结束了

本文提纲:

  1. 如果什么都不设置,默认是:父线程和子线程先后开始,当父线程、子线程都完成时,程序退出。
  2. 如果在子线程start之前,调用了setDeamon(True)方法:父子线程先后开始当父线程结束时,不论子线程是否完成,程序均退出。
  3. 如果子线程对象调用了join()方法,那么,程序运行到该处时,父线程在此处等待子线程的完成,当子线程完成时,父线程继续执行。直至父线程完成,程序退出。


主要是涉及到 join()、setDeamon()的相关内容。本文中,通过在子线程中设置sleep(10),来控制子线程和父线程完成的先后顺序,进行测试。因此,本文涉及到的代码大多数是相同的,只需要关注每段代码中的join()、setDeamon(True)、sleep(10)即可。


一、如果什么都不设置,默认是:父线程和子线程先后开始,当父线程、子线程都完成时,程序退出。


1、下面是父线程先结束的例子,可以看出,父线程已经结束,而子线程由于sleep(10),而后结束,但程序是等父子线程全部完成后,才退出。

import threading
from time import ctime,sleep



class MyThread(threading.Thread):
    def __init__(self, id):
        threading.Thread.__init__(self)
        self.id = id

    def run(self):
        x = 0
        print "son thread stard...%s"  %ctime()
        sleep(10)
        print self.id
        print "son thread end..... %s" %ctime()


if __name__ == "__main__":
    print "main is start %s " %ctime()
    t1 = MyThread(999)
    t1.start()
    for i in range(3):
        print i
    print "main thread end......."
运行结果如下:
main is start Wed Feb 07 13:02:44 2018 
son thread stard...Wed Feb 07 13:02:44 2018
0
1
2
main thread end.......
999
son thread end..... Wed Feb 07 13:02:54 2018


2、下面是子线程先结束的例子:可以看出,子线程先结束,而后父线程结束。(将1中的sleep(10)注释掉)

import threading
from time import ctime,sleep

class MyThread(threading.Thread):
    def __init__(self, id):
        threading.Thread.__init__(self)
        self.id = id

    def run(self):
        x = 0
        print "son thread stard...%s"  %ctime()
        # sleep(10)
        print self.id
        print "son thread end..... %s" %ctime()


if __name__ == "__main__":
    print "main is start %s " %ctime()
    t1 = MyThread(999)
    t1.start()
    for i in range(3):
        print i
    print "main thread end......."
运行结果如下:
main is start Wed Feb 07 13:09:04 2018 
son thread stard...Wed Feb 07 13:09:04 2018
999
0
son thread end..... Wed Feb 07 13:09:04 2018
1
2
main thread end.......

解释:由于父线程默认为非守护线程,子线程继承父线程,同样为非守护线程。python中,当所有的非守护线程都执行完时,程序结束。于是在上述情况中,只有当父子线程都结束,程序才退出。


二、如果在子线程start之前,调用了setDeamon(True)方法:父子线程先后开始,当父线程结束时,不论子线程是否完成,程序均退出。

1、下面是子线程先结束的例子:可以看出,子线程先结束,结束后,父线程随后结束。

import threading
from time import ctime,sleep

class MyThread(threading.Thread):
    def __init__(self, id):
        threading.Thread.__init__(self)
        self.id = id

    def run(self):
        x = 0
        print "son thread stard...%s"  %ctime()
        # sleep(10)
        print self.id
        print "son thread end..... %s" %ctime()


if __name__ == "__main__":
    print "main is start %s " %ctime()
    t1 = MyThread(999)
    t1.setDaemon(True)
    t1.start()
    for i in range(3):
        print i
    print "main thread end......."
运行结果如下:
main is start Wed Feb 07 13:19:13 2018 
son thread stard...Wed Feb 07 13:19:13 2018
999
0
son thread end..... Wed Feb 07 13:19:13 2018
1
2
main thread end.......

2、下面是父线程先结束的例子:可以看出,在这个例子中,父线程结束,子线程还没有结束,程序就退出。

import threading
from time import ctime,sleep

class MyThread(threading.Thread):
    def __init__(self, id):
        threading.Thread.__init__(self)
        self.id = id

    def run(self):
        x = 0
        print "son thread stard...%s"  %ctime()
        sleep(10)
        print self.id
        print "son thread end..... %s" %ctime()


if __name__ == "__main__":
    print "main is start %s " %ctime()
    t1 = MyThread(999)
    t1.setDaemon(True)
    t1.start()
    for i in range(3):
        print i
    print "main thread end......."
运行结果为:
main is start Wed Feb 07 13:25:20 2018 
son thread stard...Wed Feb 07 13:25:20 2018
0
1
2
main thread end.......

解释:由于父线程默认为非守护线程,子线在上述两个例子中被设置为守护线程。python中,当所有的非守护线程都执行完时,程序结束。于是在上述情况中,当作为非守护线程的父线程运行完时,该程序退出。


三、如果子线程对象调用了join()方法,那么,程序运行到该处时,父线程在此处等待子线程的完成,当子线程完成时,父线程继续执行。直至父线程完成,程序退出。

1、下面的代码:子线程用时比父线程要多,但父线程等子线程运行完后,才继续运行。

import threading
from time import ctime,sleep



class MyThread(threading.Thread):
    def __init__(self, id):
        threading.Thread.__init__(self)
        self.id = id

    def run(self):
        x = 0
        print "son thread stard...%s"  %ctime()
        sleep(10)
        print self.id
        print "son thread end..... %s" %ctime()


if __name__ == "__main__":
    print "main is start %s " %ctime()
    for i in range(3):
        print i
    t1 = MyThread(999)

    t1.start()
    t1.join()
    for x in range(3,5):
        print x
    print "main thread end......."

运行结果:
main is start Wed Feb 07 13:42:50 2018 
0
1
2
son thread stard...Wed Feb 07 13:42:50 2018
999
son thread end..... Wed Feb 07 13:42:50 2018
3
4
main thread end.......

解释:当子线程调用了join()方法时,父线程在join()处等待子线程执行完毕,才继续执行。等父线程也执行完,程序退出(由于父子线程均为非守护线程,因此,当父子全完时,程序才退出)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值