{"id":942445,"date":"2024-12-26T22:06:06","date_gmt":"2024-12-26T14:06:06","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/942445.html"},"modified":"2024-12-26T22:06:08","modified_gmt":"2024-12-26T14:06:08","slug":"python-atexit%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/942445.html","title":{"rendered":"python atexit\u5982\u4f55\u4f7f\u7528"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25080244\/c18b54bb-c458-4d14-9917-8f313b314e63.webp\" alt=\"python atexit\u5982\u4f55\u4f7f\u7528\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u7684atexit\u6a21\u5757\u7528\u4e8e\u5728\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u65f6\u6267\u884c\u6ce8\u518c\u7684\u6e05\u7406\u51fd\u6570\u3001\u9002\u5408\u7528\u4e8e\u8d44\u6e90\u7684\u91ca\u653e\u3001\u6570\u636e\u7684\u4fdd\u5b58\u4ee5\u53ca\u8bb0\u5f55\u65e5\u5fd7\u3002\u53ef\u4ee5\u901a\u8fc7atexit.register()\u51fd\u6570\u5c06\u6e05\u7406\u51fd\u6570\u6ce8\u518c\u5230\u7a0b\u5e8f\u7ed3\u675f\u65f6\u8c03\u7528\u7684\u51fd\u6570\u5217\u8868\u4e2d\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython atexit\u6a21\u5757\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u6ce8\u610f\u4e8b\u9879\u3002<\/strong><\/p>\n<\/p>\n<hr>\n<p><h2>\u4e00\u3001ATEEXIT\u6a21\u5757\u6982\u8ff0<\/h2>\n<\/p>\n<p><p>Python\u7684atexit\u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u6ce8\u518c\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u8981\u6267\u884c\u7684\u6e05\u7406\u51fd\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u5c06\u5728\u7a0b\u5e8f\u6b63\u5e38\u9000\u51fa\u65f6\u88ab\u8c03\u7528\uff0c\u5305\u62ec\u6b63\u5e38\u9000\u51fa\u548c\u672a\u6355\u83b7\u7684\u5f02\u5e38\u3002<\/p>\n<\/p>\n<p><h3>1\u3001\u6a21\u5757\u7684\u4f5c\u7528<\/h3>\n<\/p>\n<p><p>atexit\u6a21\u5757\u7684\u4e3b\u8981\u4f5c\u7528\u662f<strong>\u7ba1\u7406\u7a0b\u5e8f\u9000\u51fa\u65f6\u9700\u8981\u6267\u884c\u7684\u64cd\u4f5c<\/strong>\u3002\u8fd9\u5305\u62ec\u4f46\u4e0d\u9650\u4e8e\u6587\u4ef6\u5173\u95ed\u3001\u6570\u636e\u5e93\u8fde\u63a5\u5173\u95ed\u3001\u7f51\u7edc\u8fde\u63a5\u5173\u95ed\u3001\u4e34\u65f6\u6587\u4ef6\u5220\u9664\u7b49\u3002\u8fd9\u79cd\u529f\u80fd\u5728\u7f16\u5199\u9700\u8981\u4e25\u8c28<a href=\"https:\/\/docs.pingcode.com\/blog\/project-management\/58557.html\" target=\"_blank\">\u8d44\u6e90\u7ba1\u7406<\/a>\u7684\u7a0b\u5e8f\u65f6\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><h3>2\u3001atexit\u7684\u4f18\u70b9<\/h3>\n<\/p>\n<ul>\n<li><strong>\u7b80\u5316\u8d44\u6e90\u7ba1\u7406<\/strong>\uff1a\u901a\u8fc7atexit\uff0c\u53ef\u4ee5\u786e\u4fdd\u8d44\u6e90\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u88ab\u9002\u5f53\u91ca\u653e\u3002<\/li>\n<li><strong>\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong>\uff1a\u5c06\u6e05\u7406\u64cd\u4f5c\u96c6\u4e2d\u7ba1\u7406\uff0c\u4ee3\u7801\u66f4\u52a0\u6e05\u6670\u3002<\/li>\n<li><strong>\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f<\/strong>\uff1a\u907f\u514d\u56e0\u7a0b\u5e8f\u5f02\u5e38\u9000\u51fa\u800c\u5bfc\u81f4\u7684\u8d44\u6e90\u6cc4\u6f0f\u3002<\/li>\n<\/ul>\n<p><h2>\u4e8c\u3001ATEEXIT\u6a21\u5757\u7684\u57fa\u672c\u7528\u6cd5<\/h2>\n<\/p>\n<p><h3>1\u3001\u6ce8\u518c\u6e05\u7406\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528atexit.register()\u51fd\u6570\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6216\u591a\u4e2a\u6e05\u7406\u51fd\u6570\u6ce8\u518c\u5230\u7a0b\u5e8f\u7ed3\u675f\u65f6\u8c03\u7528\u7684\u51fd\u6570\u5217\u8868\u4e2d\u3002\u6ce8\u518c\u7684\u51fd\u6570\u5c06\u5728\u7a0b\u5e8f\u7ec8\u6b62\u65f6\u81ea\u52a8\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import atexit<\/p>\n<p>def cleanup():<\/p>\n<p>    print(&quot;Cleaning up resources...&quot;)<\/p>\n<p>atexit.register(cleanup)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>cleanup<\/code>\u51fd\u6570\u5c06\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6ce8\u518c\u591a\u4e2a\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u6ce8\u518c\u591a\u4e2a\u6e05\u7406\u51fd\u6570\uff0c\u5b83\u4eec\u4f1a\u6309\u7167\u6ce8\u518c\u7684\u987a\u5e8f\u6267\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import atexit<\/p>\n<p>def cleanup_one():<\/p>\n<p>    print(&quot;Cleanup One&quot;)<\/p>\n<p>def cleanup_two():<\/p>\n<p>    print(&quot;Cleanup Two&quot;)<\/p>\n<p>atexit.register(cleanup_one)<\/p>\n<p>atexit.register(cleanup_two)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>cleanup_one<\/code>\u548c<code>cleanup_two<\/code>\u51fd\u6570\u5c06\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u4f9d\u6b21\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u4f7f\u7528\u5e26\u53c2\u6570\u7684\u51fd\u6570<\/h3>\n<\/p>\n<p><p>atexit.register()\u5141\u8bb8\u6ce8\u518c\u5e26\u53c2\u6570\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def cleanup_with_args(arg1, arg2):<\/p>\n<p>    print(f&quot;Cleaning with arguments: {arg1}, {arg2}&quot;)<\/p>\n<p>atexit.register(cleanup_with_args, &quot;Argument 1&quot;, &quot;Argument 2&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\uff0c<code>cleanup_with_args<\/code>\u5c06\u4f7f\u7528\u63d0\u4f9b\u7684\u53c2\u6570\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u4e09\u3001ATEEXIT\u6a21\u5757\u7684\u9ad8\u7ea7\u7528\u6cd5<\/h2>\n<\/p>\n<p><h3>1\u3001\u53d6\u6d88\u6ce8\u518c\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u867d\u7136atexit\u672c\u8eab\u4e0d\u63d0\u4f9b\u53d6\u6d88\u6ce8\u518c\u529f\u80fd\uff0c\u4f46\u53ef\u4ee5\u901a\u8fc7\u7ba1\u7406\u6ce8\u518c\u51fd\u6570\u7684\u5f15\u7528\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import atexit<\/p>\n<p>def cleanup():<\/p>\n<p>    print(&quot;Cleanup&quot;)<\/p>\n<p>registered_function = atexit.register(cleanup)<\/p>\n<h2><strong>\u5728\u9700\u8981\u65f6\u53d6\u6d88\u6ce8\u518c<\/strong><\/h2>\n<p>if some_condition:<\/p>\n<p>    atexit._exithandlers.remove((registered_function, (), {}))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u91cc\uff0c\u6211\u4eec\u901a\u8fc7\u76f4\u63a5\u64cd\u4f5catexit\u6a21\u5757\u5185\u90e8\u7684_exithandlers\u5217\u8868\u6765\u53d6\u6d88\u6ce8\u518c\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u4e0e\u5f02\u5e38\u5904\u7406\u7ed3\u5408\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>atexit\u6a21\u5757\u5728\u5904\u7406\u672a\u6355\u83b7\u5f02\u5e38\u65f6\u975e\u5e38\u6709\u7528\uff0c\u4f46\u9700\u8981\u6ce8\u610f\uff0c\u5982\u679c\u5f02\u5e38\u5bfc\u81f4\u7a0b\u5e8f\u975e\u6b63\u5e38\u9000\u51fa\uff0c\u6ce8\u518c\u7684\u51fd\u6570\u53ef\u80fd\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002\u56e0\u6b64\uff0c\u7ed3\u5408try-except\u5757\u53ef\u4ee5\u66f4\u597d\u5730\u7ba1\u7406\u7a0b\u5e8f\u9000\u51fa\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import atexit<\/p>\n<p>def cleanup():<\/p>\n<p>    print(&quot;Cleanup&quot;)<\/p>\n<p>atexit.register(cleanup)<\/p>\n<p>try:<\/p>\n<p>    # \u7a0b\u5e8f\u4e3b\u903b\u8f91<\/p>\n<p>    r<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>se Exception(&quot;An error occurred&quot;)<\/p>\n<p>except Exception as e:<\/p>\n<p>    print(f&quot;Exception caught: {e}&quot;)<\/p>\n<p>finally:<\/p>\n<p>    print(&quot;Program exiting&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4e0e\u591a\u7ebf\u7a0b\u7ed3\u5408\u4f7f\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u591a\u7ebf\u7a0b\u73af\u5883\u4e2d\uff0catexit\u53ea\u5728\u4e3b\u7ebf\u7a0b\u7684\u7ec8\u6b62\u65f6\u6267\u884c\u3002\u56e0\u6b64\uff0c\u5728\u591a\u7ebf\u7a0b\u7a0b\u5e8f\u4e2d\u4f7f\u7528atexit\u65f6\u9700\u8981\u683c\u5916\u5c0f\u5fc3\uff0c\u786e\u4fdd\u6240\u6709\u7ebf\u7a0b\u90fd\u5df2\u9002\u5f53\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>import atexit<\/p>\n<p>def worker():<\/p>\n<p>    print(&quot;Thread working&quot;)<\/p>\n<p>def cleanup():<\/p>\n<p>    print(&quot;Cleanup&quot;)<\/p>\n<p>atexit.register(cleanup)<\/p>\n<p>thread = threading.Thread(target=worker)<\/p>\n<p>thread.start()<\/p>\n<p>thread.join()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c<code>cleanup<\/code>\u51fd\u6570\u5c06\u5728\u4e3b\u7ebf\u7a0b\u7ed3\u675f\u65f6\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001ATEEXIT\u4f7f\u7528\u6ce8\u610f\u4e8b\u9879<\/h2>\n<\/p>\n<p><h3>1\u3001\u7a0b\u5e8f\u5f02\u5e38\u9000\u51fa<\/h3>\n<\/p>\n<p><p>atexit\u6a21\u5757\u65e0\u6cd5\u5904\u7406\u6240\u6709\u5f02\u5e38\u60c5\u51b5\u3002\u7a0b\u5e8f\u5d29\u6e83\u3001\u88ab\u6740\u6b7b\u6216\u9047\u5230\u67d0\u4e9b\u7cfb\u7edf\u7ea7\u5f02\u5e38\u65f6\uff0c\u6ce8\u518c\u7684\u51fd\u6570\u53ef\u80fd\u4e0d\u4f1a\u88ab\u8c03\u7528\u3002<\/p>\n<\/p>\n<p><h3>2\u3001\u6267\u884c\u987a\u5e8f<\/h3>\n<\/p>\n<p><p>\u6ce8\u518c\u7684\u51fd\u6570\u6309\u6ce8\u518c\u987a\u5e8f\u53cd\u5411\u6267\u884c\uff0c\u8fd9\u610f\u5473\u7740\u6700\u540e\u6ce8\u518c\u7684\u51fd\u6570\u4f1a\u6700\u5148\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h3>3\u3001\u8de8\u5e73\u53f0\u884c\u4e3a<\/h3>\n<\/p>\n<p><p>\u867d\u7136atexit\u5728\u5927\u591a\u6570\u5e73\u53f0\u4e0a\u8868\u73b0\u4e00\u81f4\uff0c\u4f46\u5728\u67d0\u4e9b\u5e73\u53f0\u4e0a\u53ef\u80fd\u5b58\u5728\u5dee\u5f02\u3002\u5728\u7f16\u5199\u9700\u8981\u8de8\u5e73\u53f0\u652f\u6301\u7684\u4ee3\u7801\u65f6\uff0c\u5e94\u8fdb\u884c\u5145\u5206\u7684\u6d4b\u8bd5\u3002<\/p>\n<\/p>\n<p><h2>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h2>\n<\/p>\n<p><h3>1\u3001\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u8bb0\u5f55\u65e5\u5fd7\uff0c\u4ee5\u4fbf\u5206\u6790\u7a0b\u5e8f\u8fd0\u884c\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import atexit<\/p>\n<p>import logging<\/p>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.INFO)<\/p>\n<p>def log_exit():<\/p>\n<p>    logging.info(&quot;Program terminated&quot;)<\/p>\n<p>atexit.register(log_exit)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2\u3001\u6570\u636e\u5e93\u8fde\u63a5\u5173\u95ed<\/h3>\n<\/p>\n<p><p>\u786e\u4fdd\u5728\u7a0b\u5e8f\u9000\u51fa\u65f6\u5173\u95ed\u6570\u636e\u5e93\u8fde\u63a5\uff0c\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import atexit<\/p>\n<p>import sqlite3<\/p>\n<p>connection = sqlite3.connect(&#39;example.db&#39;)<\/p>\n<p>def close_connection():<\/p>\n<p>    connection.close()<\/p>\n<p>atexit.register(close_connection)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>3\u3001\u4e34\u65f6\u6587\u4ef6\u6e05\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u5220\u9664\u4e34\u65f6\u6587\u4ef6\uff0c\u4fdd\u6301\u6587\u4ef6\u7cfb\u7edf\u7684\u6574\u6d01\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import atexit<\/p>\n<p>import os<\/p>\n<p>temp_file = &#39;temp.txt&#39;<\/p>\n<p>def delete_temp_file():<\/p>\n<p>    if os.path.exists(temp_file):<\/p>\n<p>        os.remove(temp_file)<\/p>\n<p>atexit.register(delete_temp_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u516d\u3001\u603b\u7ed3<\/h2>\n<\/p>\n<p><p>Python\u7684atexit\u6a21\u5757\u4e3a\u7a0b\u5e8f\u9000\u51fa\u65f6\u7684\u8d44\u6e90\u7ba1\u7406\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u800c\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528atexit\uff0c\u53ef\u4ee5\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u6267\u884c\u5fc5\u8981\u7684\u6e05\u7406\u64cd\u4f5c\uff0c\u9632\u6b62\u8d44\u6e90\u6cc4\u6f0f\u548c\u6570\u636e\u4e22\u5931\u3002\u540c\u65f6\uff0c\u7ed3\u5408\u5f02\u5e38\u5904\u7406\u548c\u591a\u7ebf\u7a0b\u7ba1\u7406\uff0c\u53ef\u4ee5\u4f7f\u7a0b\u5e8f\u66f4\u52a0\u5065\u58ee\u548c\u53ef\u9760\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u811a\u672c\u8fd8\u662f\u590d\u6742\u7684\u5e94\u7528\u7a0b\u5e8f\uff0catexit\u90fd\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005\u66f4\u597d\u5730\u7ba1\u7406\u7a0b\u5e8f\u751f\u547d\u5468\u671f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u4ec0\u4e48\u662fPython\u4e2d\u7684atexit\u6a21\u5757\uff0c\u5b83\u7684\u4e3b\u8981\u529f\u80fd\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>atexit\u6a21\u5757\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u4e3b\u8981\u7528\u4e8e\u6ce8\u518c\u9000\u51fa\u51fd\u6570\u3002\u8fd9\u4e9b\u51fd\u6570\u4f1a\u5728\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u65f6\u88ab\u81ea\u52a8\u8c03\u7528\uff0c\u65e0\u8bba\u662f\u7531\u4e8e\u6b63\u5e38\u7684\u9000\u51fa\u8fd8\u662f\u901a\u8fc7\u5f02\u5e38\u5f15\u53d1\u7684\u9000\u51fa\u3002\u8fd9\u4e2a\u6a21\u5757\u53ef\u4ee5\u5e2e\u52a9\u5f00\u53d1\u8005\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\u6267\u884c\u6e05\u7406\u64cd\u4f5c\uff0c\u6bd4\u5982\u4fdd\u5b58\u72b6\u6001\u3001\u5173\u95ed\u6587\u4ef6\u6216\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<p><strong>\u5982\u4f55\u6ce8\u518c\u4e00\u4e2a\u9000\u51fa\u51fd\u6570\uff1f<\/strong><br \/>\u53ef\u4ee5\u4f7f\u7528atexit.register()\u65b9\u6cd5\u6765\u6ce8\u518c\u4e00\u4e2a\u9000\u51fa\u51fd\u6570\u3002\u4f60\u53ea\u9700\u5c06\u51fd\u6570\u540d\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u5373\u53ef\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import atexit\n\ndef cleanup():\n    print(&quot;\u6267\u884c\u6e05\u7406\u64cd\u4f5c&quot;)\n\natexit.register(cleanup)\n<\/code><\/pre>\n<p>\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\uff0ccleanup()\u51fd\u6570\u5c06\u88ab\u81ea\u52a8\u8c03\u7528\u3002<\/p>\n<p><strong>atexit\u6a21\u5757\u80fd\u5426\u6ce8\u518c\u591a\u4e2a\u9000\u51fa\u51fd\u6570\uff1f<\/strong><br \/>\u662f\u7684\uff0catexit\u6a21\u5757\u5141\u8bb8\u6ce8\u518c\u591a\u4e2a\u9000\u51fa\u51fd\u6570\u3002\u6ce8\u518c\u7684\u51fd\u6570\u5c06\u6309\u7167\u6ce8\u518c\u7684\u987a\u5e8f\u4f9d\u6b21\u6267\u884c\u3002\u5728\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u65f6\uff0c\u6240\u6709\u6ce8\u518c\u7684\u51fd\u6570\u90fd\u4f1a\u88ab\u8c03\u7528\uff0c\u786e\u4fdd\u6240\u6709\u7684\u6e05\u7406\u5de5\u4f5c\u90fd\u80fd\u987a\u5229\u5b8c\u6210\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code class=\"language-python\">import atexit\n\ndef cleanup1():\n    print(&quot;\u6e05\u7406\u64cd\u4f5c1&quot;)\n\ndef cleanup2():\n    print(&quot;\u6e05\u7406\u64cd\u4f5c2&quot;)\n\natexit.register(cleanup1)\natexit.register(cleanup2)\n<\/code><\/pre>\n<p>\u5728\u7a0b\u5e8f\u7ed3\u675f\u65f6\uff0c\u8f93\u51fa\u5c06\u662f\u201c\u6e05\u7406\u64cd\u4f5c1\u201d\u548c\u201c\u6e05\u7406\u64cd\u4f5c2\u201d\uff0c\u663e\u793a\u5b83\u4eec\u7684\u6267\u884c\u987a\u5e8f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684atexit\u6a21\u5757\u7528\u4e8e\u5728\u7a0b\u5e8f\u6b63\u5e38\u7ec8\u6b62\u65f6\u6267\u884c\u6ce8\u518c\u7684\u6e05\u7406\u51fd\u6570\u3001\u9002\u5408\u7528\u4e8e\u8d44\u6e90\u7684\u91ca\u653e\u3001\u6570\u636e\u7684\u4fdd\u5b58\u4ee5\u53ca\u8bb0\u5f55 [&hellip;]","protected":false},"author":3,"featured_media":942446,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/942445"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=942445"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/942445\/revisions"}],"predecessor-version":[{"id":942447,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/942445\/revisions\/942447"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/942446"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=942445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=942445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=942445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}