Thread 类构造器参数及说明:
def __init__(self, group=None, target=None, name=None,
args=(), kwargs=None, *, daemon=None):
"""This constructor should always be called with keyword arguments. Arguments are:
*group* should be None; reserved for future extension when a ThreadGroup
class is implemented.
*target* is the callable object to be invoked by the run()
method. Defaults to None, meaning nothing is called.
*name* is the thread name. By default, a unique name is constructed of
the form "Thread-N" where N is a small decimal number.
*args* is the argument tuple for the target invocation. Defaults to ().
*kwargs* is a dictionary of keyword arguments for the target
invocation. Defaults to {}.
If a subclass overrides the constructor, it must make sure to invoke
the base class constructor (Thread.__init__()) before doing anything
else to the thread.
"""
assert group is None, "group argument must be None for now"
if kwargs is None:
kwargs = {}
self._target = target
self._name = str(name or _newname())
self._args = args
self._kwargs = kwargs
if daemon is not None:
self._daemonic = daemon
else:
self._daemonic = current_thread().daemon
其中比较关键的两个参数:
target一般是一个函数;
daemon 指定所创建的线程是否为后代线程。如果没有指定则继承父线程的这个属性。
daemon是守护的意思,当daemon赋值为True时,进程不会等到线程的结束,进程一结束,线程也会结束;当daemon赋值为False时,进程会等线程执行结束再退出。打个比方,daemon为False,就相当于领导等员工活干完了,才下班;daemon为True,就相当于领导一走,员工也马上撤了,如果不设置,默认领导是会等员工再走的。
————————————————
版权声明:本文为优快云博主「SummerMiko_」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/SummerMiko_/article/details/126821386
Thread类构造器详解
本文详细解析了Thread类构造器的各个参数及其作用,重点介绍了target参数和daemon参数的使用方式及含义。target参数通常是一个待执行的函数,而daemon参数用于设定线程是否为守护线程。
1680

被折叠的 条评论
为什么被折叠?



