sys.exit实际上对多个线程有什么作用? [英] What does sys.exit really do with multiple threads?

查看:95
本文介绍了sys.exit实际上对多个线程有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的被python中的sys.exit()弄糊涂了. 在 python文档中,显示为从Python退出";这是否意味着在python程序中调用sys.exit()时,进程将退出?如果是这样,下面的代码将显示不同的结果:

I was really confused by sys.exit() in python. In python documentation, it says "Exit from Python"; does that mean when sys.exit() is called in a python program, the process will exit? If so, the code below shows a different result:

import sys
import time
import threading

def threadrun():
    while(True):
        time.sleep(1)

if __name__=="__main__":
    t=threading.Thread(target=threadrun)
    t.start()
    sys.exit()

在linux上启动该程序,结果不是python文档所说的预期结果,但仍然可以在系统中运行,所以sys.exit()到底能做什么?

Launching this program in linux, result was not the expected one as python documentation says but still run in the system, so what does sys.exit() really do?

推荐答案

根据文档 sys.exit() 引发SystemExit:

通过提高SystemExit(status)退出解释器.

Exit the interpreter by raising SystemExit(status).

如果SystemExit到达默认异常处理程序, 它会调用handle_system_exit(),或多或少会推送到 Py_Finalize() ,依次调用 wait_for_thread_shutdown() 在Python 2中,因此sys.exit()与正常的从主模块底部掉出的相同,它等待所有非守护进程线程终止.

If SystemExit reaches the default exception handler, it calls handle_system_exit(), which more or less pushes through to Py_Finalize(), which in turn calls wait_for_thread_shutdown() in Python 2, so sys.exit() is the same as the normal falling off the bottom of the main module in waiting for all non-daemon threads to terminate.

这篇关于sys.exit实际上对多个线程有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆