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,就相当于领导一走,员工也马上撤了,如果不设置,默认领导是会等员工再走的。
————————————————
版权声明:本文为CSDN博主「SummerMiko_」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/SummerMiko_/article/details/126821386