Python: is thread still running

0 votes

How can I see whether a thread has completed? I tried the following code:

import thread
import threading

id1 = thread.start_new_thread(my_function, ())
#wait some time
threads_list = threading.enumerate()
# Want to know if my_function() that was called by thread id1 has returned 

def my_function()
    #do stuff
    return

but threads_list does not contain the thread that was started, even when I know the thread is still running. Can anyone help me solve this issue?

Aug 5 in Python by ana1504.k
• 7,890 points
20 views

1 answer to this question.

0 votes

The key is to start the thread using threading, not thread:

t1 = threading.Thread(target=my_function, args=())
t1.start()

Then use

z = t1.isAlive()

or

l = threading.enumerate()
answered Aug 5 by SDeb
• 13,190 points

Related Questions In Python

0 votes
1 answer

What is the process to kill a particular thread in python?

A multiprocessing.Process can p.terminate() In the cases where I want to ...READ MORE

answered Feb 4 in Python by charlie_brown
• 7,720 points
228 views
0 votes
1 answer

Python error "Python NameError: global name 'Thread' is not defined"

There is a built-in function with the ...READ MORE

answered May 31 in Python by Riya
348 views
+2 votes
3 answers

what is the practical use of polymorphism in Python?

Polymorphism is the ability to present the ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
1,626 views
+4 votes
7 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 351 views
0 votes
1 answer

How is Python 2.7.3 and Python 3.3 different?

raw_input() is not used in Python 3. Use input()  ...READ MORE

answered Sep 12, 2018 in Python by SDeb
• 13,190 points
38 views
0 votes
1 answer

comparing strings in Python using "==" or "is"

is is used for identity testing and ...READ MORE

answered Sep 19, 2018 in Python by SDeb
• 13,190 points
42 views