os._exit()用于退出当前进程中的主线程
sys.exit()用于退出当前线程
在另起的进程中使用os._exit()
import os
import threading
import time
from multiprocessing import Process
from threading import Thread
def name():
thread = threading.current_thread()
return thread.getName()
def one():
i = 1
while True:
i += 1
time.sleep(3)
print("---one---", name())
if i == 5:
print("======os._exit======")
os._exit(0)
def two():
while True:
time.sleep(3)
print("---two---", name())
def process():
t1 = Thread(target=one, args=())
t2 = Thread(target=two, args=())
t1.start()
t2.start()
while True:
time.sleep(3)
print("---process---", name())
def main():
p1 = Process(target=process)
p1.start()
while True:
time.sleep(3)
print("---main---", name())
if __name__ == "__main__":
main()
执行结果:
当前程序终止在process函数,也就是主函数main()中的p1进程终止,也就是p1进程中的主线程,将会在执行os._exit()后,被终止。但是不会影响主进程中的主线程,比如main()函数会继续执行。
在另起的线程中使用os._exit()
import os
import threading
import time
from threading import Thread
def name():
thread = threading.current_thread()
return thread.getName()
def one():
i = 1
while True:
i += 1
time.sleep(3)
print("---one---", name())
if i == 5:
print("======os._exit======")
os._exit(0)
def two():
while True:
time.sleep(3)
print("---two---", name())
def process():
t1 = Thread(target=one, args=())
t2 = Thread(target=two, args=())
t1.start()
t2.start()
while True:
time.sleep(3)
print("---process---", name())
def main():
p1 = Thread(target=process)
p1.start()
while True:
time.sleep(3)
print("---main---", name())
if __name__ == "__main__":
main()
执行结果:
当前程序终止在process函数,也就是主函数main()中的p1线程终止,也就是p1线程中的主线程,将会在执行os._exit()后,被终止,p1线程的主线程是main()中的线程,所以主线程也会结束。
另起进程使用sys.exit()
import sys
import threading
import time
from multiprocessing import Process
from threading import Thread
def name():
thread = threading.current_thread()
return thread.getName()
def one():
i = 1
while True:
i += 1
time.sleep(3)
print("---one---", name())
if i == 5:
print("======sys.exit======")
sys.exit(0)
def two():
while True:
time.sleep(3)
print("---two---", name())
def process():
t1 = Thread(target=one, args=())
t2 = Thread(target=two, args=())
t1.start()
t2.start()
while True:
time.sleep(3)
print("---process---", name())
def main():
p1 = Process(target=process)
p1.start()
while True:
time.sleep(3)
print("---main---", name())
if __name__ == "__main__":
main()
执行结果:
执行sys.exit()后,sys.exit()所在的函数线程终止,也就是one()线程终止,但是不会影响其他的子线程和主线程或者其他进程。
另起线程使用sys.exit()
import sys
import threading
import time
from threading import Thread
def name():
thread = threading.current_thread()
return thread.getName()
def one():
i = 1
while True:
i += 1
time.sleep(3)
print("---one---", name())
if i == 5:
print("======sys.exit======")
sys.exit(0)
def two():
while True:
time.sleep(3)
print("---two---", name())
def process():
t1 = Thread(target=one, args=())
t2 = Thread(target=two, args=())
t1.start()
t2.start()
while True:
time.sleep(3)
print("---process---", name())
def main():
p1 = Thread(target=process)
p1.start()
while True:
time.sleep(3)
print("---main---", name())
if __name__ == "__main__":
main()
执行结果:
另起线程和另起进程对于sys.exit()来说结果都是一样的,sys.exit()只会对当前的线程进行操作,当前运行是子线程,就会停止子线程,当前运行的是主线程,就会停止主线程。