{"id":207675,"date":"2025-07-08T12:03:50","date_gmt":"2025-07-08T04:03:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207675\/"},"modified":"2025-07-08T12:03:50","modified_gmt":"2025-07-08T04:03:50","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8%e5%87%bd%e6%95%b0%e8%b0%83%e7%94%a8%e9%93%be%e4%b8%ad%e4%bd%bf%e7%94%a8-context-context-%e5%b9%b6%e5%8f%96%e6%b6%88","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207675\/","title":{"rendered":"\u5982\u4f55\u5728\u51fd\u6570\u8c03\u7528\u94fe\u4e2d\u4f7f\u7528 context.Context \u5e76\u53d6\u6d88"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u5982\u4f55\u5728\u51fd\u6570\u8c03\u7528\u94fe\u4e2d\u4f7f\u7528 context.Context \u5e76\u53d6\u6d88<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-24 15:27:36<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300a\u5982\u4f55\u5728\u51fd\u6570\u8c03\u7528\u94fe\u4e2d\u4f7f\u7528 context.Context \u5e76\u53d6\u6d88\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5047\u8bbe\u6211\u6709\u8fd9\u4e9b\u529f\u80fd\uff1a<\/p>\n<pre>func A(ctx context.Context) {\n    \/\/ A takes some time to process\n}\n\nfunc B(ctx context.Context) {\n    \/\/ B takes some time to process\n}\n\nfunc C(ctx context.Context) {\n    \/\/ C takes some time to process\n}<\/pre>\n<p><strong>\u6ce8\u610f\uff1a\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u90fd\u9700\u8981\u4e00\u4e9b\u65f6\u95f4\u6765\u5904\u7406\u3002<\/strong><\/p>\n<p><strong>\u6ce8\u610f\uff1ab \u5e94\u8be5\u5728\u51fd\u6570 a \u5185\u90e8\u8c03\u7528\uff0cc \u5e94\u8be5\u5728\u51fd\u6570 b \u5185\u90e8\u8c03\u7528\u3002<\/strong><\/p>\n<p>\u53ea\u9700\u8981\u4e00\u4e2a\u9488\u5bf9\u8fd9\u79cd\u7279\u5b9a\u60c5\u51b5\u7684\u793a\u4f8b\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ea\u9700\u4f20\u9012\u4e0e\u53c2\u6570\u76f8\u540c\u7684\u4e0a\u4e0b\u6587\uff0c\u4f46\u6bcf\u5f53\u60a8\u7b49\u5f85\u6765\u81ea\u901a\u9053\\\u8fdc\u7a0b\u670d\u52a1\u5668\\\u4efb\u4f55\u5185\u5bb9\u7684\u7ed3\u679c\u65f6\uff0c\u60a8\u90fd\u53ef\u4ee5\u4f7f\u7528 select \u8bed\u53e5\u6765\u7b49\u5f85 &lt;- ctx.Done()\u3002\u6bcf\u5f53\u4e0a\u4e0b\u6587\u88ab\u53d6\u6d88\uff08\u901a\u8fc7\u53d6\u6d88\u51fd\u6570\u6216\u7531\u4e8e\u8d85\u65f6\uff09\u65f6\uff0c\u5185\u90e8\u901a\u9053\u5c06\u88ab\u5173\u95ed\uff0c\u5e76\u4e14\u63a5\u6536\u64cd\u4f5c\u59cb\u7ec8\u5728\u5173\u95ed\u7684\u901a\u9053\u4e0a\u89e3\u9664\u963b\u585e\u3002 <\/p>\n<p>\u5173\u4e8e\u53d6\u6d88\u7684\u4e00\u822c\u5e16\u5b50\uff08\u6ca1\u6709\u4e0a\u4e0b\u6587\uff0c\u4f46\u5728\u5fc3\u91cc\u7528 ctx.Done() \u66ff\u6362 didCh\uff09 <\/p>\n<p>\u8c08\u8bba\u53d6\u6d88\uff08\u7b2c 15 \u9875\u7684\u4e0a\u4e0b\u6587\u793a\u4f8b\uff09 <\/p>\n<p>\u51e0\u4e2a\u4f8b\u5b50 <\/p>\n<p>\u672c\u7bc7\u5173\u4e8e\u300a\u5982\u4f55\u5728\u51fd\u6570\u8c03\u7528\u94fe\u4e2d\u4f7f\u7528 context.Context \u5e76\u53d6\u6d88\u300b\u7684\u4ecb\u7ecd\u5c31\u5230\u6b64\u7ed3\u675f\u5566\uff0c\u4f46\u662f\u5b66\u65e0\u6b62\u5883\uff0c\u60f3\u8981\u4e86\u89e3\u5b66\u4e60\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207675","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207675"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207675\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}