{"id":3596,"date":"2025-06-01T15:09:52","date_gmt":"2025-06-04T08:03:51","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=3596"},"modified":"2025-06-04T15:09:52","modified_gmt":"2025-06-04T08:03:51","slug":"%e8%a7%a3%e6%b1%bapython%e4%b8%adinterruptederror%e7%9a%84%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/python\/%e8%a7%a3%e6%b1%bapython%e4%b8%adinterruptederror%e7%9a%84%e6%96%b9%e6%b3%95\/","title":{"rendered":"2025 \u5e74\u6700\u65b0\u89e3\u6c7a Python \u4e2d InterruptedError \u7684\u65b9\u6cd5"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"Python, InterruptedError, Python \u6559\u5b78, Python \u4f8b\u5916\u8655\u7406\"><\/p>\n<h1>\u6df1\u5165\u89e3\u6790 Python InterruptedError \u53ca\u5176\u89e3\u6c7a\u65b9\u6848<\/h1>\n<p>\u5728 Python \u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c**InterruptedError** \u662f\u4e00\u500b\u5e38\u898b\u4f46\u91cd\u8981\u7684\u4f8b\u5916\u3002\u7576\u60a8\u7684\u7a0b\u5f0f\u57f7\u884c\u904e\u7a0b\u4e2d\u88ab\u4e2d\u65b7\u6642\uff0c\u9019\u500b\u4f8b\u5916\u6703\u88ab\u62cb\u51fa\uff0c\u8868\u793a\u7a0b\u5f0f\u4e0d\u6703\u7e7c\u7e8c\u57f7\u884c\u3002\u9019\u53ef\u80fd\u662f\u56e0\u70ba\u7528\u6236\u624b\u52d5\u4e2d\u65b7\uff08\u4f8b\u5982\u6309\u4e0b Ctrl + C\uff09\u6216\u7cfb\u7d71\u767c\u51fa\u4e2d\u65b7\u4fe1\u865f\uff08\u5982 SIGINT \u6216 SIGTERM\uff09\u3002<\/p>\n<p>### InterruptedError \u4f8b\u5916\u7684\u4f86\u6e90<\/p>\n<p>* **\u7528\u6236\u4e2d\u65b7**\uff1a\u7576\u7528\u6236\u5728\u7d42\u7aef\u4e2d\u6309\u4e0b Ctrl + C \u6642\uff0cPython \u6703\u62cb\u51fa\u6b64\u4f8b\u5916\u3002<br \/>\n* **\u7cfb\u7d71\u4fe1\u865f**\uff1a\u7576\u7cfb\u7d71\u767c\u9001 SIGINT \u6216 SIGTERM \u4fe1\u865f\u6642\uff0c\u4e5f\u5c07\u5c0e\u81f4\u8a72\u4f8b\u5916\u7684\u767c\u751f\u3002<\/p>\n<p>### \u5982\u4f55\u6355\u6349 InterruptedError<\/p>\n<p>\u6355\u6349\u9019\u500b\u4f8b\u5916\u7684\u6a19\u6e96\u505a\u6cd5\u662f\u4f7f\u7528 `try\/except` \u8a9e\u53e5\u3002\u4ee5\u4e0b\u662f\u57fa\u672c\u7684\u5be6\u4f5c\u7bc4\u4f8b\uff1a<\/p>\n<p>&#8220;`python<br \/>\ntry:<br \/>\n    # \u60a8\u7684\u4e3b\u8981\u7a0b\u5f0f\u78bc<br \/>\nexcept InterruptedError:<br \/>\n    print(&#8220;\u7a0b\u5f0f\u5df2\u88ab\u4e2d\u65b7\uff0c\u57f7\u884c\u76f8\u61c9\u7684\u64cd\u4f5c\u3002&#8221;)<br \/>\n&#8220;`<\/p>\n<p>### \u4f7f\u7528 signal \u6a21\u7d44\u4f86\u8655\u7406\u4e2d\u65b7<\/p>\n<p>\u60a8\u4e5f\u53ef\u4ee5\u5229\u7528 Python \u7684 `signal` \u6a21\u7d44\u4f86\u6355\u6349\u4e2d\u65b7\u4fe1\u865f\u3002\u4ee5\u4e0b\u662f\u793a\u7bc4\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport signal<\/p>\n<p>def handler(signum, frame):<br \/>\n    print(&#8220;\u4e2d\u65b7\u4fe1\u865f\u63a5\u6536\uff0c\u57f7\u884c\u6e05\u7406\u5de5\u4f5c\u3002&#8221;)<\/p>\n<p>signal.signal(signal.SIGINT, handler)<br \/>\n# \u958b\u59cb\u57f7\u884c\u7a0b\u5f0f<br \/>\nwhile True:<br \/>\n    pass  # \u9019\u88e1\u662f\u60a8\u7684\u7a0b\u5f0f\u908f\u8f2f<br \/>\n&#8220;`<\/p>\n<p>### \u4f7f\u7528 atexit \u6a21\u7d44\u9032\u884c\u6e05\u7406<\/p>\n<p>`atexit` \u6a21\u7d44\u53ef\u4ee5\u7528\u4f86\u5728\u7a0b\u5f0f\u6b63\u5e38\u7d50\u675f\u6216\u4e2d\u65b7\u6642\u57f7\u884c\u7279\u5b9a\u64cd\u4f5c\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport atexit<\/p>\n<p>def cleanup():<br \/>\n    print(&#8220;\u7a0b\u5f0f\u7d50\u675f\uff0c\u57f7\u884c\u6e05\u7406\u5de5\u4f5c\u3002&#8221;)<\/p>\n<p>atexit.register(cleanup)<br \/>\n# \u958b\u59cb\u57f7\u884c\u7a0b\u5f0f<br \/>\nwhile True:<br \/>\n    pass  # \u9019\u88e1\u662f\u60a8\u7684\u7a0b\u5f0f\u908f\u8f2f<br \/>\n&#8220;`<\/p>\n<p>### \u4f7f\u7528 contextlib \u6a21\u7d44<\/p>\n<p>\u4f7f\u7528 `contextlib` \u53ef\u4ee5\u66f4\u512a\u96c5\u5730\u7ba1\u7406\u4e2d\u65b7\u60c5\u6cc1\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport contextlib<\/p>\n<p>@contextlib.contextmanager<br \/>\ndef handle_interrupt():<br \/>\n    try:<br \/>\n        yield<br \/>\n    except InterruptedError:<br \/>\n        print(&#8220;\u4e2d\u65b7\u767c\u751f\uff0c\u57f7\u884c\u6e05\u7406\u3002&#8221;)<\/p>\n<p>with handle_interrupt():<br \/>\n    # \u958b\u59cb\u57f7\u884c\u7a0b\u5f0f<br \/>\n    while True:<br \/>\n        pass  # \u9019\u88e1\u662f\u60a8\u7684\u7a0b\u5f0f\u908f\u8f2f<br \/>\n&#8220;`<\/p>\n<p>### \u4f7f\u7528 threading \u6a21\u7d44<\/p>\n<p>\u5982\u679c\u60a8\u7684\u7a0b\u5f0f\u4f7f\u7528\u591a\u57f7\u884c\u7dd2\uff0c\u60a8\u53ef\u4ee5\u9019\u6a23\u8655\u7406\uff1a<\/p>\n<p>&#8220;`python<br \/>\nimport threading<\/p>\n<p>def thread_handler():<br \/>\n    print(&#8220;\u57f7\u884c\u4e2d\u65b7\u8655\u7406\u3002&#8221;)<\/p>\n<p>threading.Thread(target=thread_handler).start()<br \/>\n# \u958b\u59cb\u57f7\u884c\u7a0b\u5f0f<br \/>\nwhile True:<br \/>\n    pass  # \u9019\u88e1\u662f\u60a8\u7684\u7a0b\u5f0f\u908f\u8f2f<br \/>\n&#8220;`<\/p>\n<p>### \u7e3d\u7d50<\/p>\n<p>**InterruptedError** \u662f\u4e00\u500b\u95dc\u9375\u7684\u4f8b\u5916\uff0c\u4e86\u89e3\u5b83\u7684\u8655\u7406\u65b9\u5f0f\u53ef\u4ee5\u5e6b\u52a9\u60a8\u66f4\u597d\u5730\u638c\u63a7\u7a0b\u5f0f\u7684\u57f7\u884c\u6d41\u7a0b\u3002\u7121\u8ad6\u662f\u4f7f\u7528 `try\/except` \u9084\u662f\u5176\u4ed6\u6a21\u7d44\uff0c\u5408\u7406\u7684\u8655\u7406\u65b9\u5f0f\u90fd\u80fd\u63d0\u5347\u7a0b\u5f0f\u7684\u7a69\u5b9a\u6027\u8207\u4f7f\u7528\u8005\u9ad4\u9a57\u3002<\/p>\n<p>\u5982\u9700\u9032\u4e00\u6b65\u5b78\u7fd2 Python \u7684\u4f8b\u5916\u8655\u7406\uff0c\u63a8\u85a6\u53c3\u8003 [Python \u4f8b\u5916\u8655\u7406\u6559\u5b78](https:\/\/vocus.cc\/article\/64b8e3c30f27e800019a11e3)\u3002<\/p>\n<p>### Q&#038;A\uff08\u5e38\u898b\u554f\u984c\u89e3\u7b54\uff09<\/p>\n<p>**Q1: InterruptedError \u662f\u4ec0\u9ebc\uff1f**<br \/>\nA1: InterruptedError \u662f\u4e00\u7a2e\u4f8b\u5916\uff0c\u7576\u7a0b\u5f0f\u88ab\u4e2d\u65b7\u6642\u6703\u88ab\u62cb\u51fa\uff0c\u901a\u5e38\u7531\u7528\u6236\u6216\u7cfb\u7d71\u4fe1\u865f\u5f15\u8d77\u3002<\/p>\n<p>**Q2: \u5982\u4f55\u8655\u7406 InterruptedError\uff1f**<br \/>\nA2: \u53ef\u4ee5\u4f7f\u7528 `try\/except` \u8a9e\u53e5\u3001`signal` \u6a21\u7d44\u3001`atexit` \u6a21\u7d44\u7b49\u591a\u7a2e\u65b9\u5f0f\u4f86\u6355\u6349\u548c\u8655\u7406\u6b64\u4f8b\u5916\u3002<\/p>\n<p>**Q3: \u6709\u54ea\u4e9b\u6a21\u7d44\u53ef\u4ee5\u7528\u4f86\u6355\u6349\u4e2d\u65b7\uff1f**<br \/>\nA3: \u53ef\u4ee5\u4f7f\u7528 `signal`\u3001`atexit`\u3001`contextlib` \u548c `threading` \u6a21\u7d44\u4f86\u6355\u6349\u548c\u8655\u7406 InterruptedError\u3002<\/p>\n<p>&#8212;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39Python\u4e2dInterruptedError\u7684\u89e3\u6c7a\u65b9\u6cd5\uff0c\u5305\u62ec\u4f7f\u7528methods()\u51fd\u6578\u4f86\u6355\u7372InterruptedError\uff0c\u4ee5\u53ca\u5982\u4f55\u4f7f\u7528try-except\u8a9e\u53e5\u4f86\u8655\u7406InterruptedError\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-3596","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-W0","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3596","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=3596"}],"version-history":[{"count":2,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3596\/revisions"}],"predecessor-version":[{"id":12786,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/3596\/revisions\/12786"}],"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=3596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=3596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=3596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}