{"id":132,"date":"2025-02-19T06:36:51","date_gmt":"2025-02-19T06:36:51","guid":{"rendered":"https:\/\/burningthepasttvt.damo.sg\/?p=132"},"modified":"2025-02-19T06:36:51","modified_gmt":"2025-02-19T06:36:51","slug":"%e6%a0%88%e6%ba%a2%e5%87%ba%e9%95%bf%e5%ba%a6%e6%a3%80%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/burningthepasttvt.damo.sg\/?p=132","title":{"rendered":"\u6808\u6ea2\u51fa\u957f\u5ea6\u68c0\u67e5"},"content":{"rendered":"\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7528\u4e8e\u68c0\u67e5\u6808\u6ea2\u51fa\u957f\u5ea6\u7684Python\u811a\u672c\u3002\u8be5\u811a\u672c\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u6a21\u62df\u6808\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u5e76\u5728\u63a5\u8fd1\u6808\u6ea2\u51fa\u65f6\u8f93\u51fa\u76f8\u5173\u4fe1\u606f\uff1a<\/p>\n\n\n\n<p>python<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import sys\nimport logging\n\n<em># \u914d\u7f6e\u65e5\u5fd7<\/em>\nlogging.basicConfig(filename='stack_usage.log', level=logging.INFO)\n\ndef get_current_depth():\n    \"\"\"\n    \u83b7\u53d6\u5f53\u524d\u6808\u7684\u6df1\u5ea6\u3002\n    \"\"\"\n    depth = 0\n    while True:\n        try:\n            depth += 1\n            sys._getframe(depth)\n        except ValueError:\n            break\n    return depth\n\ndef monitor_stack_usage(current_depth, max_depth=1000):\n    \"\"\"\n    \u76d1\u63a7\u6808\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u9632\u6b62\u6808\u6ea2\u51fa\u3002\n    \"\"\"\n    current_depth += 1\n    print(f\"\u5f53\u524d\u6808\u6df1\u5ea6: {current_depth}\")\n    \n    <em># \u5982\u679c\u63a5\u8fd1\u6808\u6ea2\u51fa\u9650\u5236\uff0c\u53d1\u51fa\u8b66\u544a<\/em>\n    warning_threshold = max_depth * 0.9  <em># \u5f53\u6808\u6df1\u5ea6\u8fbe\u523090%\u65f6\u53d1\u51fa\u8b66\u544a<\/em>\n    if current_depth &gt;= warning_threshold:\n        logging.warning(f\"\u6808\u6df1\u5ea6\u63a5\u8fd1\u9650\u5236\uff0c\u5f53\u524d\u6df1\u5ea6: {current_depth}, \u6700\u5927\u9650\u5236: {max_depth}\")\n        print(f\"\u8b66\u544a\uff1a\u6808\u6df1\u5ea6 {current_depth} \u5df2\u63a5\u8fd1\u6700\u5927\u9650\u5236 {max_depth}\uff01\")\n    \n    if current_depth &lt; max_depth:\n        monitor_stack_usage(current_depth, max_depth)\n    else:\n        print(\"\u6808\u6ea2\u51fa\u5373\u5c06\u53d1\u751f\uff01\")\n\ndef check_stack_usage():\n    \"\"\"\n    \u68c0\u67e5\u6808\u7684\u4f7f\u7528\u60c5\u51b5\u3002\n    \"\"\"\n    print(\"\u5f00\u59cb\u68c0\u67e5\u6808\u7684\u4f7f\u7528\u60c5\u51b5...\")\n    max_depth = sys.getrecursionlimit()\n    print(f\"\u5f53\u524d\u6808\u7684\u6700\u5927\u9012\u5f52\u6df1\u5ea6\u9650\u5236: {max_depth}\")\n    \n    try:\n        monitor_stack_usage(0, max_depth)\n    except RecursionError:\n        logging.error(\"\u6808\u6ea2\u51fa\u53d1\u751f\uff01\", exc_info=True)\n        print(\"\u6808\u6ea2\u51fa\u53d1\u751f\uff01\u8bf7\u68c0\u67e5\u65e5\u5fd7\u6587\u4ef6\uff1astack_usage.log\")\n\nif __name__ == \"__main__\":\n    check_stack_usage()\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u529f\u80fd\u8bf4\u660e\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u83b7\u53d6\u5f53\u524d\u6808\u6df1\u5ea6<\/strong>\u00a0\uff1a\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528\u00a0<code>sys._getframe(depth)<\/code>\u00a0\u65b9\u6cd5\u6765\u83b7\u53d6\u5f53\u524d\u6808\u7684\u6df1\u5ea6\u3002<\/li>\n\n\n\n<li><code>get_current_depth()<\/code>\u00a0\u51fd\u6570\u4f1a\u8fd4\u56de\u5f53\u524d\u6808\u7684\u5b9e\u9645\u6df1\u5ea6\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u76d1\u63a7\u6808\u7684\u4f7f\u7528\u60c5\u51b5<\/strong>\u00a0\uff1a\n<ul class=\"wp-block-list\">\n<li><code>monitor_stack_usage()<\/code>\u00a0\u51fd\u6570\u4f1a\u9012\u5f52\u8c03\u7528\u81ea\u8eab\uff0c\u5e76\u5728\u6bcf\u6b21\u8c03\u7528\u65f6\u68c0\u67e5\u5f53\u524d\u6808\u6df1\u5ea6\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u6808\u6df1\u5ea6\u63a5\u8fd1\u9650\u5236\uff08\u9ed8\u8ba4\u4e3a90%\uff09\uff0c\u4f1a\u53d1\u51fa\u8b66\u544a\u5e76\u8bb0\u5f55\u65e5\u5fd7\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u6808\u6ea2\u51fa\u68c0\u67e5<\/strong>\u00a0\uff1a\n<ul class=\"wp-block-list\">\n<li><code>check_stack_usage()<\/code>\u00a0\u51fd\u6570\u4f1a\u521d\u59cb\u5316\u68c0\u67e5\u8fc7\u7a0b\uff0c\u5e76\u6355\u83b7\u6808\u6ea2\u51fa\u5f02\u5e38\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528\u65b9\u6cd5\uff1a<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4fdd\u5b58\u4ee3\u7801\u4e3a\u00a0<code>stack_usage_check.py<\/code>\u3002<\/li>\n\n\n\n<li>\u8fd0\u884c\u811a\u672c\uff1a<code>python stack_usage_check.py<\/code>\u3002<\/li>\n\n\n\n<li>\u89c2\u5bdf\u8f93\u51fa\u548c\u65e5\u5fd7\u6587\u4ef6\u00a0<code>stack_usage.log<\/code>\u3002<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">\u6ce8\u610f\u4e8b\u9879\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Python\u7684\u9ed8\u8ba4\u9012\u5f52\u6df1\u5ea6\u9650\u5236\u4e3a1000\u5c42\uff08\u53ef\u4ee5\u901a\u8fc7\u00a0<code>sys.getrecursionlimit()<\/code>\u00a0\u67e5\u770b\uff09\u3002<\/li>\n\n\n\n<li>\u5982\u679c\u9700\u8981\u66f4\u5927\u7684\u9012\u5f52\u6df1\u5ea6\uff0c\u53ef\u4ee5\u901a\u8fc7\u00a0<code>sys.setrecursionlimit()<\/code>\u00a0\u8bbe\u7f6e\uff0c\u4f46\u4e0d\u5efa\u8bae\u8bbe\u7f6e\u8fc7\u5927\uff0c\u4ee5\u514d\u5bfc\u81f4\u7a0b\u5e8f\u5d29\u6e83\u3002<\/li>\n\n\n\n<li>\u811a\u672c\u4f1a\u5728\u6808\u6df1\u5ea6\u63a5\u8fd1\u9650\u5236\u65f6\u53d1\u51fa\u8b66\u544a\uff0c\u5e2e\u52a9\u4f60\u53ca\u65f6\u53d1\u73b0\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u4f60\u53ef\u4ee5\u6839\u636e\u9700\u8981\u8c03\u6574&nbsp;<code>warning_threshold<\/code>&nbsp;\u7684\u503c\uff08\u4f8b\u5982\u6539\u4e3a80%\u621695%\uff09\u6765\u9002\u5e94\u4e0d\u540c\u7684\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7528\u4e8e\u68c0\u67e5\u6808\u6ea2\u51fa\u957f\u5ea6\u7684Python\u811a\u672c\u3002\u8be5\u811a\u672c\u901a\u8fc7\u9012\u5f52\u51fd\u6570\u6a21\u62df\u6808\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u5e76\u5728\u63a5\u8fd1\u6808\u6ea2\u51fa\u65f6\u8f93\u51fa\u76f8\u5173\u4fe1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=132"}],"version-history":[{"count":1,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=\/wp\/v2\/posts\/132\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/burningthepasttvt.damo.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}