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, 2019 in Python by ana1504.k
• 7,910 points
5,603 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, 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,720 points
3,720 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, 2019 in Python by Riya
7,303 views
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,770 points
1,072 views
0 votes
0 answers

How to know which Python is running in Jupyter notebook?

I am using jupyter notebook and I ...READ MORE

Apr 24, 2022 in Python by Kichu
• 19,040 points
585 views
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 4,443 views
0 votes
1 answer
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
928 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,300 points
842 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP