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

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, 2019 in Python by ana1504.k
• 7,910 points

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=())

Then use

z = t1.isAlive()


l = threading.enumerate()
answered Aug 5, 2019 by SDeb
• 13,300 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, 2019 in Python by charlie_brown
• 7,780 points
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, 2019 in Python by Riya
0 votes
1 answer

What is the best online python compiler for running python scripts?

You can use Microsoft azure notebooks to ...READ MORE

answered Oct 14, 2020 in Python by anonymous
• 65,950 points
+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,440 points
0 votes
2 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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,414 views
0 votes
0 answers
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,300 points
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,300 points