{"id":3631,"date":"2025-06-03T15:25:45","date_gmt":"2025-06-04T07:41:45","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3631"},"modified":"2025-06-04T15:25:45","modified_gmt":"2025-06-04T07:41:45","slug":"%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84resourcewarning%e8%ad%a6%e5%91%8a%e5%8f%8a%e5%85%b6%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e4%ba%86%e8%a7%a3python%e4%b8%ad%e7%9a%84resourcewarning%e8%ad%a6%e5%91%8a%e5%8f%8a%e5%85%b6%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%b3%95\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3Python\u4e2d\u7684ResourceWarning\u8b66\u544a\u53ca\u5176\u6700\u4f73\u89e3\u6c7a\u65b9\u6848\uff082025\u5e74\u7248\uff09"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, ResourceWarning, Python \u6559\u5b78, Python \u932f\u8aa4\u6392\u9664\"><\/p>\n<h1>\u6df1\u5165\u4e86\u89e3Python\u4e2d\u7684ResourceWarning\u8b66\u544a\u53ca\u5176\u6700\u4f73\u89e3\u6c7a\u65b9\u6848<\/h1>\n<p>Python\u7684ResourceWarning\u662f\u4e00\u7a2e\u91cd\u8981\u7684\u8b66\u544a\uff0c\u65e8\u5728\u63d0\u9192\u958b\u767c\u8005\u5728\u7a0b\u5f0f\u78bc\u4e2d\u4f7f\u7528\u4e86\u53ef\u80fd\u5c0e\u81f4\u8cc7\u6e90\u6cc4\u9732\u6216\u4e0d\u5b89\u5168\u64cd\u4f5c\u7684\u8cc7\u6e90\u3002\u9019\u7a2e\u8b66\u544a\u81eaPython 3.4\u7248\u672c\u8d77\u5f15\u5165\uff0c\u4e26\u5728\u5f8c\u7e8c\u7248\u672c\u4e2d\u5f97\u5230\u52a0\u5f37\u3002\u7406\u89e3\u548c\u8655\u7406\u9019\u4e9b\u8b66\u544a\u5c0d\u65bc\u78ba\u4fdd\u7a0b\u5f0f\u78bc\u7684\u5065\u58ef\u6027\u548c\u6027\u80fd\u81f3\u95dc\u91cd\u8981\u3002<\/p>\n<h2>\u4ec0\u9ebc\u662fResourceWarning\uff1f<\/h2>\n<p>ResourceWarning\u8868\u793a\u5728Python\u7a0b\u5f0f\u4e2d\u767c\u73fe\u4e86\u4e0d\u5b89\u5168\u7684\u8cc7\u6e90\u4f7f\u7528\u60c5\u6cc1\uff0c\u9019\u53ef\u80fd\u6703\u5c0e\u81f4\u57f7\u884c\u6548\u7387\u4f4e\u4e0b\u6216\u8cc7\u6e90\u6cc4\u9732\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u898b\u7684\u89f8\u767c\u60c5\u6cc1\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u4e0d\u5b89\u5168\u7684\u7db2\u8def\u9023\u7dda\uff08\u5982\u672a\u95dc\u9589\u7684socket\uff09<\/li>\n<li>\u672a\u6b63\u78ba\u95dc\u9589\u7684\u6a94\u6848\u64cd\u4f5c<\/li>\n<li>\u4e0d\u5b89\u5168\u7684\u8cc7\u6599\u5eab\u9023\u7dda\uff08\u5982\u672a\u95dc\u9589\u7684cursor\uff09<\/li>\n<li>\u4e0d\u7576\u4f7f\u7528\u7cfb\u7d71\u547c\u53eb\uff08\u5982subprocess\u672a\u95dc\u9589\u7684\u7ba1\u9053\uff09<\/li>\n<\/ul>\n<p>\u9019\u4e9b\u4e0d\u5b89\u5168\u7684\u8cc7\u6e90\u4f7f\u7528\u53ef\u80fd\u5c0e\u81f4\u4ee5\u4e0b\u554f\u984c\uff1a<\/p>\n<ul>\n<li>\u7a0b\u5f0f\u78bc\u57f7\u884c\u6642\u9593\u904e\u9577\u6216\u4e0d\u7a69\u5b9a<\/li>\n<li>\u8cc7\u6e90\u88ab\u610f\u5916\u91cb\u653e\u6216\u6d29\u9732<\/li>\n<li>\u5c0e\u81f4\u5f8c\u7e8c\u64cd\u4f5c\u5931\u6557\u6216\u7570\u5e38<\/li>\n<\/ul>\n<p>\u56e0\u6b64\uff0c\u958b\u767c\u8005\u61c9\u8a72\u8b39\u614e\u8655\u7406\u9019\u4e9b\u8b66\u544a\uff0c\u4ee5\u7dad\u8b77\u7a0b\u5f0f\u78bc\u7684\u8cea\u91cf\u3002<\/p>\n<h2>\u5982\u4f55\u89e3\u6c7aResourceWarning\uff1f<\/h2>\n<p>\u8981\u89e3\u6c7aResourceWarning\uff0c\u958b\u767c\u8005\u9700\u6aa2\u67e5\u7a0b\u5f0f\u78bc\uff0c\u627e\u51fa\u4e26\u4fee\u6b63\u4e0d\u5b89\u5168\u7684\u8cc7\u6e90\u4f7f\u7528\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6709\u6548\u7684\u89e3\u6c7a\u65b9\u6848\uff1a<\/p>\n<p>1. **\u78ba\u4fdd\u6b63\u78ba\u95dc\u9589\u8cc7\u6e90**\uff1a\u4f7f\u7528`with`\u8a9e\u53e5\u81ea\u52d5\u7ba1\u7406\u8cc7\u6e90\u7684\u958b\u555f\u548c\u95dc\u9589\u3002<\/p>\n<p>&#8220;`python<br \/>\nwith open(&#8216;example.txt&#8217;, &#8216;r&#8217;) as file:<br \/>\n    data = file.read()<br \/>\n# file\u6703\u81ea\u52d5\u95dc\u9589<br \/>\n&#8220;`<\/p>\n<p>2. **\u4f7f\u7528\u5b89\u5168\u7684\u7db2\u8def\u9023\u7dda**\uff1a\u78ba\u4fddsocket\u9023\u7dda\u5728\u4f7f\u7528\u5b8c\u7562\u5f8c\u6b63\u78ba\u95dc\u9589\u3002<\/p>\n<p>&#8220;`python<br \/>\nimport socket<\/p>\n<p>sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)<br \/>\ntry:<br \/>\n    sock.connect((&#8216;localhost&#8217;, 12345))<br \/>\n    # \u57f7\u884c\u7db2\u8def\u64cd\u4f5c<br \/>\nfinally:<br \/>\n    sock.close()  # \u78ba\u4fddsocket\u95dc\u9589<br \/>\n&#8220;`<\/p>\n<p>3. **\u4f7f\u7528logging\u6a21\u7d44\u8a18\u9304\u8b66\u544a**\uff1a\u9019\u6a23\u53ef\u4ee5\u5728\u5f8c\u7e8c\u67e5\u627e\u548c\u89e3\u6c7a\u554f\u984c\u6642\u63d0\u4f9b\u5e6b\u52a9\u3002<\/p>\n<p>&#8220;`python<br \/>\nimport logging<\/p>\n<p>logging.basicConfig(level=logging.WARNING)<br \/>\nlogging.warning(&#8216;ResourceWarning detected, please check your resource usage.&#8217;)<br \/>\n&#8220;`<\/p>\n<h2>\u5982\u4f55\u9632\u6b62ResourceWarning\uff1f<\/h2>\n<p>\u9632\u6b62ResourceWarning\u7684\u6700\u4f73\u505a\u6cd5\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u59cb\u7d42\u4f7f\u7528`with`\u8a9e\u53e5\u4f86\u8655\u7406\u6a94\u6848\u548c\u7db2\u8def\u9023\u7dda<\/li>\n<li>\u78ba\u4fdd\u5728\u4f7f\u7528\u5b8c\u8cc7\u6599\u5eab\u9023\u7dda\u5f8c\u95dc\u9589cursor<\/li>\n<li>\u907f\u514d\u4e0d\u5fc5\u8981\u7684\u5168\u5c40\u8b8a\u91cf\uff0c\u6e1b\u5c11\u8cc7\u6e90\u7684\u6301\u7e8c\u5360\u7528<\/li>\n<\/ul>\n<p>\u5be6\u65bd\u9019\u4e9b\u7b56\u7565\u53ef\u4ee5\u986f\u8457\u6e1b\u5c11ResourceWarning\u7684\u51fa\u73fe\uff0c\u4e26\u63d0\u9ad8\u7a0b\u5f0f\u78bc\u7684\u53ef\u7dad\u8b77\u6027\u548c\u6027\u80fd\u3002<\/p>\n<h2>\u7d50\u8ad6<\/h2>\n<p>ResourceWarning\u662fPython\u958b\u767c\u4e2d\u4e0d\u53ef\u5ffd\u8996\u7684\u8b66\u544a\uff0c\u7406\u89e3\u5176\u610f\u7fa9\u548c\u89e3\u6c7a\u65b9\u6cd5\u5c0d\u65bc\u63d0\u5347\u7a0b\u5f0f\u78bc\u8cea\u91cf\u81f3\u95dc\u91cd\u8981\u3002\u901a\u904e\u9075\u5faa\u6700\u4f73\u5be6\u8e10\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6709\u6548\u89e3\u6c7a\u548c\u9632\u6b62\u9019\u4e9b\u8b66\u544a\u7684\u51fa\u73fe\uff0c\u5f9e\u800c\u78ba\u4fdd\u61c9\u7528\u7a0b\u5f0f\u7684\u7a69\u5b9a\u6027\u548c\u6548\u7387\u3002<\/p>\n<p>\u5982\u679c\u60f3\u8981\u9032\u4e00\u6b65\u4e86\u89e3Python\u7684\u958b\u767c\u6280\u5de7\uff0c\u53ef\u4ee5\u53c3\u8003 [\u9019\u88e1\u7684\u6559\u5b78](https:\/\/vocus.cc\/article\/646d8acb6e9a570015c7c5a4) \u4ee5\u7372\u53d6\u66f4\u591a\u5be6\u7528\u8cc7\u6e90\u3002<\/p>\n<h2>\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff08Q&#038;A\uff09<\/h2>\n<h3>1. ResourceWarning\u6703\u5f71\u97ff\u7a0b\u5f0f\u78bc\u57f7\u884c\u55ce\uff1f<\/h3>\n<p>\u662f\u7684\uff0cResourceWarning\u901a\u5e38\u8868\u793a\u7a0b\u5f0f\u78bc\u4e2d\u5b58\u5728\u6f5b\u5728\u7684\u8cc7\u6e90\u7ba1\u7406\u554f\u984c\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u6548\u80fd\u4e0b\u964d\u6216\u8cc7\u6e90\u6cc4\u9732\uff0c\u61c9\u53ca\u6642\u8655\u7406\u3002<\/p>\n<h3>2. \u5982\u4f55\u67e5\u770b\u6240\u6709\u7684ResourceWarning\uff1f<\/h3>\n<p>\u53ef\u4ee5\u4f7f\u7528Python\u7684`warnings`\u6a21\u7d44\u4f86\u63a7\u5236\u548c\u986f\u793a\u6240\u6709\u7684\u8b66\u544a\uff0c\u5305\u62ecResourceWarning\u3002\u4f7f\u7528`warnings.simplefilter(&#8216;always&#8217;, ResourceWarning)`\u53ef\u4ee5\u5f37\u5236\u986f\u793a\u6240\u6709ResourceWarning\u3002<\/p>\n<h3>3. \u662f\u5426\u6709\u5de5\u5177\u53ef\u4ee5\u5e6b\u52a9\u6211\u6aa2\u67e5ResourceWarning\uff1f<\/h3>\n<p>\u662f\u7684\uff0c\u4f7f\u7528\u975c\u614b\u4ee3\u78bc\u5206\u6790\u5de5\u5177\u5982`pylint`\u6216`mypy`\u53ef\u4ee5\u81ea\u52d5\u6aa2\u67e5\u7a0b\u5f0f\u78bc\u4e2d\u7684\u6f5b\u5728\u554f\u984c\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u8b58\u5225\u4e26\u4fee\u6b63ResourceWarning\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39Python\u4e2d\u7684ResourceWarning\u8b66\u544a\uff0c\u4e26\u63d0\u4f9b\u6709\u95dc\u5982\u4f55\u89e3\u6c7a\u8a72\u8b66\u544a\u7684\u6307\u5c0e\u3002\u8b80\u8005\u5c07\u5b78\u7fd2\u5982\u4f55\u4f7f\u7528Python\u4e2d\u7684methods()\u51fd\u6578\uff0c\u4ee5\u53ca\u5982\u4f55\u907f\u514d\u51fa\u73feResourceWarning\u8b66\u544a\u3002<\/p>\n","protected":false},"author":1,"featured_media":2518,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[18],"tags":[15],"class_list":["post-3631","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/badgameshow.com\/steven\/wp-content\/uploads\/2022\/12\/DALL\u00b7E-2022-12-28-14.25.55-\u62f7\u8c9d2.png","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-Wz","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=3631"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3631\/revisions"}],"predecessor-version":[{"id":12774,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3631\/revisions\/12774"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media\/2518"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=3631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}