python使用Thread的setDaemon启动后台线程

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m0_37422289/article/details/80696761

多线程编程当中, 线程的存在形态比较抽象. 通过前台线程\后台线程, 可以有效理解线程运行顺序.(复杂的多线程程序可以通过设置线程优先级实现)
后台线程与前台线程的直接区别是,
1)setDaemon(True): 当主线程退出时,后台线程随机退出;
2)setDaemon(False)(默认情况): 当主线程退出时,若前台线程还未结束,则等待所有线程结束,相当于在程序末尾加入join().

实例:
例子描述:主线程调用giveures给出字符串s的md5摘要,同时在giveures当中启动一个线程打印字符串内容.

1.前台线程

import time
from hashlib import md5
from threading import Thread

def pmd(md):
    time.sleep(3) #使用sleep使得该线程比主线程晚结束
    print("backend recording:",md)


def giveures(s):
    md = md5(s.encode('utf-8'))
    res = md.digest()
    t = Thread(target=pmd,args=(s,))
    #t.setDaemon(True) 默认情况:t.setDaemon(False)
    t.start()
    return res

s = 'chrisyang'
res = giveures(s)
print(res)
exit()

运行结果:

b'h#\x86|\xa0\xeff\xc7u\xba\x18\xb2\xd2s\xf9\x9e'
backend recording: chrisyang

第一行打印出来之后,隔3s后打印出第二行,说明在等待pmd线程结束后进程才退出.

2.后台线程

import time
from hashlib import md5
from threading import Thread

def pmd(md):
    time.sleep(3) #使用sleep使得该线程比主线程晚结束
    print("backend recording:",md)


def giveures(s):
    md = md5(s.encode('utf-8'))
    res = md.digest()
    t = Thread(target=pmd,args=(s,))
    t.setDaemon(True)
    t.start()
    return res

s = 'chrisyang'
res = giveures(s)
print(res)
exit()

运行结果:

b'h#\x86|\xa0\xeff\xc7u\xba\x18\xb2\xd2s\xf9\x9e'

pmd线程因为sleep挂起一段时间,因此在主线程完成时还未结束就被强制退出了.

知识归纳:

以上的是前后台线程的外在区别,至于内在区别,转自Stack Overflow的一个精炼回答:
后台线程的责任是为整个主线程提供服务,如保持网络连接(发送keep-alive心跳包),负责内存管理与垃圾回收(实际上JVM就是这样做的). 因此这些线程与实际提供应用服务的线程有了逻辑上的”前/后”的概念,而如果主线程已经退出,那么后台线程也没有存在的必要.
如果没有这一机制,那么我们在主线程完成之后,还必须逐个地检查后台线程,然后在主线程退出之前,逐个地关闭它们. 有了前后线程的区分, 我们只需要负责管理前台线程, 完成主要的逻辑处理之后退出即可.

原文:

Daemon Threads Explanation

Some threads do background tasks, like sending keepalive packets, or performing periodic garbage collection, or whatever. These are only useful when the main program is running, and it’s okay to kill them off once the other, non-daemon, threads have exited.

Without daemon threads, you’d have to keep track of them, and tell them to exit, before your program can completely quit. By setting them as daemon threads, you can let them run and forget about them, and when your program quits, any daemon threads are killed automatically.

猜你喜欢

转载自blog.csdn.net/m0_37422289/article/details/80696761