👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
This could be a stupid question, but I"m testing out some of my assumptions about Python and I"m confused as to why the following code snippet would not exit when called in the thread, but would exit when called in the main thread.
import sys, time
from threading import Thread
def testexit():
time.sleep(5)
sys.exit()
print "post thread exit"
t = Thread(target = testexit)
t.start()
t.join()
print "pre main exit, post thread exit"
sys.exit()
print "post main exit"
The docs for sys.exit() state that the call should exit from Python. I can see from the output of this program that "post thread exit" is never printed, but the main thread just keeps on going even after the thread calls exit.
Is a separate instance of the interpreter being created for each thread, and the call to exit() is just exiting that separate instance? If so, how does the threading implementation manage access to shared resources? What if I did want to exit the program from the thread (not that I actually want to, but just so I understand)?
👻 Read also: what is the best laptop for engineering students?
We hope this article has helped you to resolve the problem. Apart from Why does sys.exit() not exit when called inside a thread in Python?, check other __main__ Python module-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Why does sys.exit() not exit when called inside a thread in Python?
- Deutsch Why does sys.exit() not exit when called inside a thread in Python?
- Français Why does sys.exit() not exit when called inside a thread in Python?
- Español Why does sys.exit() not exit when called inside a thread in Python?
- Türk Why does sys.exit() not exit when called inside a thread in Python?
- Русский Why does sys.exit() not exit when called inside a thread in Python?
- Português Why does sys.exit() not exit when called inside a thread in Python?
- Polski Why does sys.exit() not exit when called inside a thread in Python?
- Nederlandse Why does sys.exit() not exit when called inside a thread in Python?
- 中文 Why does sys.exit() not exit when called inside a thread in Python?
- 한국어 Why does sys.exit() not exit when called inside a thread in Python?
- 日本語 Why does sys.exit() not exit when called inside a thread in Python?
- हिन्दी Why does sys.exit() not exit when called inside a thread in Python?
California | 2023-03-29
Maybe there are another answers? What Why does sys.exit() not exit when called inside a thread in Python? exactly means?. I am just not quite sure it is the best method
Rome | 2023-03-29
Thanks for explaining! I was stuck with Why does sys.exit() not exit when called inside a thread in Python? for some hours, finally got it done 🤗. Will use it in my bachelor thesis
Texas | 2023-03-29
Thanks for explaining! I was stuck with Why does sys.exit() not exit when called inside a thread in Python? for some hours, finally got it done 🤗. Will get back tomorrow with feedback