{"id":207863,"date":"2025-07-08T12:10:14","date_gmt":"2025-07-08T04:10:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207863\/"},"modified":"2025-07-08T12:10:14","modified_gmt":"2025-07-08T04:10:14","slug":"%e9%87%87%e7%94%a8%e4%b8%8a%e4%b8%8b%e6%96%87%e5%8f%96%e6%b6%88%e7%9a%84%e5%bc%82%e6%ad%a5-%e7%ad%89%e5%be%85%e6%a8%a1%e5%bc%8f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207863\/","title":{"rendered":"\u91c7\u7528\u4e0a\u4e0b\u6587\u53d6\u6d88\u7684\u5f02\u6b65\/\u7b49\u5f85\u6a21\u5f0f"},"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>\u91c7\u7528\u4e0a\u4e0b\u6587\u53d6\u6d88\u7684\u5f02\u6b65\/\u7b49\u5f85\u6a21\u5f0f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-26 10:21:39<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u6765\u5230\u7684\u5927\u5bb6\uff0c\u76f8\u4fe1\u90fd\u662f\u7f16\u7a0b\u5b66\u4e60\u7231\u597d\u8005\uff0c\u5e0c\u671b\u5728\u8fd9\u91cc\u5b66\u4e60Golang\u76f8\u5173\u7f16\u7a0b\u77e5\u8bc6\u3002\u4e0b\u9762\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u5e26\u5927\u5bb6\u804a\u804a\u300a\u91c7\u7528\u4e0a\u4e0b\u6587\u53d6\u6d88\u7684\u5f02\u6b65\/\u7b49\u5f85\u6a21\u5f0f\u300b\uff0c\u4ecb\u7ecd\u4e00\u4e0b\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5728\u6211\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u6211\u9700\u8981\u5177\u6709\u4e0a\u4e0b\u6587\u53d6\u6d88\u652f\u6301\u7684\u5f02\u6b65\/\u7b49\u5f85\u6a21\u5f0f\u3002\u5728\u5b9e\u8df5\u4e2d\uff0c\u6211\u6709\u4e00\u4e2a\u7c7b\u4f3c\u7684\u51fd\u6570\uff1a<\/p>\n<pre>func longrunningtask() &lt;-chan int32 {\n    r := make(chan int32)\n\n    go func() {\n        defer close(r)\n        \n        \/\/ simulate a workload.\n        time.sleep(time.second * 3)\n        r &lt;- rand.int31n(100)\n    }()\n\n    return r\n}<\/pre>\n<p>\u4f46\u662f\uff0c\u5b83\u4e0d\u652f\u6301\u4e0a\u4e0b\u6587\u53d6\u6d88\u3002\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a\u53c2\u6570\u5e76\u4fee\u6539\u51fd\u6570\u6765\u7b49\u5f85 select \u8bed\u53e5\u4e2d\u7684 <code>ctx.done()<\/code> \u901a\u9053\u4fe1\u53f7\uff0c\u4ee5\u4fbf\u5728\u53d6\u6d88\u4e0a\u4e0b\u6587\u65f6\u4e2d\u6b62\u64cd\u4f5c\u3002<\/p>\n<p>\u5982\u679c\u8fd9\u6837\u505a\uff0c\u5982\u679c\u8fd0\u884c\u4e24\u6b21\u6216\u591a\u6b21\uff0c\u8be5\u51fd\u6570\u5c06\u65e0\u6cd5\u6b63\u786e\u4e2d\u6b62\uff08\u56e0\u4e3a\u4e0a\u4e0b\u6587\u6307\u9488\u5c06\u88ab\u5171\u4eab\uff09\uff0c\u56e0\u4e3a\u4e0a\u4e0b\u6587\u53d6\u6d88\u901a\u9053\u4ec5\u63a5\u6536<em>\u4e00\u4e2a<\/em>\u4fe1\u53f7\uff1a<\/p>\n<pre>ctx := ...\ngo func() { r := &lt;-longrunningtask(ctx) } \/\/ done() works\ngo func() { r := &lt;-longrunningtask(ctx) } \/\/ ?\n\/\/ cancel() ...<\/pre>\n<p>\u4ee5\u4e0b\u662f\u6211\u5bf9\u201c\u5b8c\u6210\u201d\u7684\u770b\u6cd5\uff1a<\/p>\n<pre>\/\/ go\/context.go\n\n   357  func (c *cancelCtx) Done() &lt;-chan struct{} {\n   358      c.mu.Lock()\n   359      if c.done == nil {\n   360          c.done = make(chan struct{})\n   361      }\n   362      d := c.done\n   363      c.mu.Unlock()\n   364      return d\n   365  } \/\/ Done() returns the same channel for all callers, and cancellation signal is sent once only<\/pre>\n<ol>\n<li>\n<p>go \u6e90\u4ee3\u7801\u662f\u5426\u610f\u5473\u7740 <code>context<\/code> \u5e76\u4e0d\u771f\u6b63\u652f\u6301\u8c03\u7528\u5176\u4ed6\u201c\u957f\u65f6\u95f4\u8fd0\u884c\u201d\u51fd\u6570\u7684\u51fd\u6570\u7684\u4e2d\u6b62\uff0c\u5373\u201c\u94fe\u5f0f\u53d6\u6d88\u201d\uff1f<\/p>\n<\/li>\n<li>\n<p>\u6709\u54ea\u4e9b\u9009\u9879\u53ef\u4ee5\u7f16\u5199\u5f02\u6b65\u51fd\u6570\u6765\u652f\u6301 <code>.done()<\/code> \u4f7f\u7528\u7684\u65e0\u9650\u9012\u5f52\u4e2d\u7684\u4e0a\u4e0b\u6587\u53d6\u6d88\uff1f<\/p>\n<\/li>\n<\/ol>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<ol>\n<li>go \u6e90\u4ee3\u7801\u662f\u5426\u610f\u5473\u7740\u4e0a\u4e0b\u6587\u5e76\u4e0d\u771f\u6b63\u652f\u6301\u4e2d\u6b62 a \u8c03\u7528\u5176\u4ed6\u201c\u957f\u65f6\u95f4\u8fd0\u884c\u201d\u51fd\u6570\u7684\u51fd\u6570\uff0c\u201c\u94fe\u5f0f\u51fd\u6570\u201d \u53d6\u6d88\u201d\uff1f<\/li>\n<\/ol>\n<p>\u6ca1\u6709\u3002\u4efb\u52a1\u53ef\u4ee5\u8c03\u7528\u5176\u4ed6\u957f\u65f6\u95f4\u8fd0\u884c\u7684\u4efb\u52a1\uff0c\u5e76\u6cbf\u8c03\u7528\u94fe\u4f20\u9012\u4e0a\u4e0b\u6587\u3002\u8fd9\u662f\u6807\u51c6\u505a\u6cd5\u3002\u5982\u679c\u4e0a\u4e0b\u6587\u88ab\u53d6\u6d88\uff0c\u5d4c\u5957\u8c03\u7528\u5c06\u51fa\u9519\u5e76\u6cbf\u7740\u8c03\u7528\u5806\u6808\u5192\u51fa\u53d6\u6d88\u9519\u8bef<\/p>\n<ol>\n<li>\u6709\u54ea\u4e9b\u9009\u9879\u53ef\u4ee5\u7f16\u5199\u5f02\u6b65\u51fd\u6570\u6765\u652f\u6301\u65e0\u9650\u9012\u5f52\u4f7f\u7528 .done() \u65f6\u7684\u4e0a\u4e0b\u6587\u53d6\u6d88\uff1f<\/li>\n<\/ol>\n<p>\u9012\u5f52\u4e0e\u91c7\u7528\u4e0a\u4e0b\u6587\u7684\u51e0\u4e2a\u5d4c\u5957\u8c03\u7528\u6ca1\u6709\u4ec0\u4e48\u4e0d\u540c\u3002\u5982\u679c\u9012\u5f52\u8c03\u7528\u91c7\u7528\u4e0a\u4e0b\u6587\u8f93\u5165\u53c2\u6570\u5e76\u8fd4\u56de\u9519\u8bef\uff08\u5373\u68c0\u67e5\uff09\uff0c\u5219\u9012\u5f52\u8c03\u7528\u94fe\u5c06\u50cf\u4e00\u7ec4\u975e\u9012\u5f52\u5d4c\u5957\u8c03\u7528\u4e00\u6837\u5192\u6ce1\u53d6\u6d88\u4e8b\u4ef6\u3002<\/p>\n<p>\u9996\u5148\uff0c\u8ba9\u6211\u4eec\u66f4\u65b0\u60a8\u7684\u5305\u88c5\u51fd\u6570\u4ee5\u652f\u6301 <code>context.context<\/code>\uff1a<\/p>\n<pre>func longrunningtask(ctx context.context) &lt;-chan int32 {\n    r := make(chan int32)\n\n    go func() {\n        defer close(r)\n        \n        \/\/ workload\n        i, err := somework(ctx)\n        if err != nil {\n            return\n        }\n        r &lt;- i\n    }()\n\n    return r\n}<\/pre>\n<p>\u7136\u540e <code>somework<\/code> &#8211; \u4f7f\u7528\u7761\u7720\u5de5\u4f5c\u8d1f\u8f7d\u5c06\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>func somework(ctx context.context) (int32, error) {\n\n    tc := time.after(3*time.second) \/\/ fake workload\n\n    \/\/ we can check this \"workload\" *and* the context at the same time\n    select {\n    case &lt;-tc:\n        return rand.int31n(100), nil\n    case &lt;-ctx.done():\n        return 0, ctx.err()\n    }\n}<\/pre>\n<p>\u8fd9\u91cc\u8981\u6ce8\u610f\u7684\u91cd\u8981\u4e00\u70b9\u662f\uff0c\u6211\u4eec\u53ef\u4ee5\u6539\u53d8\u5047\u201c\u5de5\u4f5c\u8d1f\u8f7d\u201d\uff08time.sleep\uff09\uff0c\u4f7f\u5176\u6210\u4e3a\u4e00\u4e2a\u901a\u9053 &#8211; \u4ece\u800c\u901a\u8fc7 <code>select<\/code> \u8bed\u53e5\u89c2\u5bdf\u5b83\u548c\u6211\u4eec\u7684\u4e0a\u4e0b\u6587\u3002\u5927\u591a\u6570\u5de5\u4f5c\u8d1f\u8f7d\u5f53\u7136\u4e0d\u662f\u8fd9\u4e48\u5fae\u4e0d\u8db3\u9053&#8230;<\/p>\n<p>\u5e78\u8fd0\u7684\u662f\uff0cgo \u6807\u51c6\u5e93\u5b8c\u5168\u652f\u6301 <code>context.context<\/code>\u3002\u56e0\u6b64\uff0c\u5982\u679c\u60a8\u7684\u5de5\u4f5c\u8d1f\u8f7d\u7531\u5927\u91cf\u53ef\u80fd\u957f\u65f6\u95f4\u8fd0\u884c\u7684 sql \u67e5\u8be2\u7ec4\u6210\uff0c\u5219\u53ef\u4ee5\u5411\u6bcf\u4e2a\u67e5\u8be2\u4f20\u9012\u4e00\u4e2a\u4e0a\u4e0b\u6587\u3002\u4e0e http \u8bf7\u6c42\u6216 grpc \u8c03\u7528\u76f8\u540c\u3002\u5982\u679c\u60a8\u7684\u5de5\u4f5c\u8d1f\u8f7d\u7531\u4efb\u4f55\u8fd9\u4e9b\u8c03\u7528\u7ec4\u6210\uff0c\u5219\u4f20\u5165\u7236\u4e0a\u4e0b\u6587\u5c06\u5bfc\u81f4\u4efb\u4f55\u8fd9\u4e9b\u6f5c\u5728\u7684\u963b\u585e\u8c03\u7528\u5728\u4e0a\u4e0b\u6587\u53d6\u6d88\u65f6\u8fd4\u56de\u9519\u8bef &#8211; \u56e0\u6b64\u60a8\u7684\u5de5\u4f5c\u8d1f\u8f7d\u5c06\u8fd4\u56de\u53d6\u6d88\u9519\u8bef\uff0c\u8ba9\u8c03\u7528\u8005\u77e5\u9053\u4ec0\u4e48\u53d1\u751f\u4e86\u3002<\/p>\n<p>\u5982\u679c\u60a8\u7684\u5de5\u4f5c\u8d1f\u8f7d\u4e0d\u5b8c\u5168\u9002\u5408\u6b64\u6a21\u578b\uff0c\u4f8b\u5982\u8ba1\u7b97\u5927\u578b\u66fc\u5fb7\u5c14\u5e03\u7f57\u7279\u96c6\u56fe\u50cf\u3002\u5728\u6bcf\u4e2a\u50cf\u7d20\u4e4b\u540e\u68c0\u67e5\u53d6\u6d88\u7684\u4e0a\u4e0b\u6587\u53ef\u80fd\u4f1a\u5bf9\u6027\u80fd\u4ea7\u751f\u8d1f\u9762\u5f71\u54cd\uff0c\u56e0\u4e3a\u8f6e\u8be2\u9009\u62e9\u4e0d\u662f\u514d\u8d39\u7684\uff1a<\/p>\n<pre>select {\ncase &lt;-ctx.Done(): \/\/ polling select when `default` is included\n    return ctx.Err()\ndefault:\n}<\/pre>\n<p>\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u5e94\u7528\u8c03\u6574\uff0c\u5982\u679c\u50cf\u7d20\u4ee5 10,000\/s \u7684\u901f\u7387\u8ba1\u7b97 &#8211; \u6bcf 10,000 \u4e2a\u50cf\u7d20\u8f6e\u8be2\u4e00\u6b21\u4e0a\u4e0b\u6587\u5c06\u786e\u4fdd\u4efb\u52a1\u4ece\u53d6\u6d88\u70b9\u8d77\u4e0d\u665a\u4e8e 1 \u79d2\u8fd4\u56de\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8eGolang\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/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-207863","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207863","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=207863"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207863\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}