You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中@atexit.register定义的回调在sys.exit时未执行的原因?

为什么Python中使用@atexit.register定义的回调函数调用sys.exit()时未执行?

我遇到一个问题:在Python中用@atexit.register装饰器注册了回调函数,但调用sys.exit()时这个函数并没有执行。

以下是测试代码:

import atexit
import sys
if __name__ == '__main__':
    @atexit.register
    def Func1():
        print("Func1!!!")
    sys.exit(0)

运行后没有输出预期的Func1!!!。

我的运行环境:

  • IDE:Anaconda Navigator中的Spyder,搭配IPython 8.12.0
  • 解释器:Python 3.11.4

我已经尝试过的操作:通过Google搜索相关问题,并且查阅了Python官方的atexit模块文档。

恳请各位帮忙解答,非常感谢。

内容的提问来源于stack exchange,提问作者黃奕捷

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 00:19:52