Reputation: 980
I'm trying to implement some basic threading in my script and I need to check whether the thread already exist or not, I've found how to set names but cannot figure out how to use is_alive function by name
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()
how can I check later is thread name5 is alive or not?
Upvotes: 0
Views: 201
Reputation: 879083
The is_alive
method does not take any arguments. You don't use is_alive
by name. Instead, just call t.is_alive()
to check if the thread t
is alive.
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():
...
PS. The docs say the name attribute,
... is a string used for identification purposes only. It has no semantics. Multiple threads may be given the same name.
so don't rely on the name as a definitive means of identification.
Upvotes: 2