Python 按名称访问线程
我试图在我的脚本中实现一些基本的线程,我需要检查线程是否已经存在,我已经找到了如何设置名称,但无法找到如何按名称使用is_alive函数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
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():
...
医生说
。。。仅用于标识目的的字符串。它没有
语义多个线程可能具有相同的名称
因此,不要将姓名作为确定身份的手段