sys.exit()和os._exit()在进程和线程中使用的区别

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()只会对当前的线程进行操作,当前运行是子线程,就会停止子线程,当前运行的是主线程,就会停止主线程。
在这里插入图片描述

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值