Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 按名称访问线程_Python_Multithreading - Fatal编程技术网

Python 按名称访问线程

Python 按名称访问线程,python,multithreading,Python,Multithreading,我试图在我的脚本中实现一些基本的线程,我需要检查线程是否已经存在,我已经找到了如何设置名称,但无法找到如何按名称使用is_alive函数 class History(threading.Thread): def __init__(self): threading.Thread.__init__(self) def run(self): #do some stuff for i in range(10): t = History

我试图在我的脚本中实现一些基本的线程,我需要检查线程是否已经存在,我已经找到了如何设置名称,但无法找到如何按名称使用is_alive函数

class History(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)

    def run(self):
        #do some stuff

for i in range(10):
    t = History
    t.setName("name%s"%i))
    t().start()

以后如何检查线程名5是否处于活动状态?

方法
处于活动状态。您不能按名称使用
is\u alive
。相反,只需调用
t.is\u alive()
检查线程
t
是否处于活动状态

class History(threading.Thread):
    def __init__(self,*args,**kwargs):
        threading.Thread.__init__(self,*args,**kwargs)

    def run(self):
        #do some stuff
    
threads=[History(name="name%s"%i) for i in range(10)]
for t in threads:
    t.start()

while threads[5].is_alive():
    ...
医生说

。。。仅用于标识目的的字符串。它没有 语义多个线程可能具有相同的名称

因此,不要将姓名作为确定身份的手段