{"id":207297,"date":"2025-07-08T08:33:07","date_gmt":"2025-07-08T00:33:07","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207297\/"},"modified":"2025-07-08T08:33:07","modified_gmt":"2025-07-08T00:33:07","slug":"%e5%a6%82%e4%bd%95%e5%a4%84%e7%90%86%e5%8f%af%e4%bb%a5%e5%a2%9e%e9%95%bf%e8%80%8c%e4%b8%8d%e4%bc%9a%e9%98%bb%e5%a1%9e%e7%9a%84%e9%98%9f%e5%88%97","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207297\/","title":{"rendered":"\u5982\u4f55\u5904\u7406\u53ef\u4ee5\u589e\u957f\u800c\u4e0d\u4f1a\u963b\u585e\u7684\u961f\u5217"},"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\u5904\u7406\u53ef\u4ee5\u589e\u957f\u800c\u4e0d\u4f1a\u963b\u585e\u7684\u961f\u5217<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 15:42:35<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u662f\u7ed3\u5408\u6211\u4e4b\u524d\u9762\u8bd5\u7684\u5404\u79cd\u7ecf\u5386\u548c\u5b9e\u6218\u5f00\u53d1\u4e2d\u9047\u5230\u7684\u95ee\u9898\u89e3\u51b3\u7ecf\u9a8c\u6574\u7406\u7684\uff0c\u5e0c\u671b\u8fd9\u7bc7\u300a\u5982\u4f55\u5904\u7406\u53ef\u4ee5\u589e\u957f\u800c\u4e0d\u4f1a\u963b\u585e\u7684\u961f\u5217\u300b\u5bf9\u4f60\u6709\u5f88\u5927\u5e2e\u52a9\uff01\u6b22\u8fce\u6536\u85cf\uff0c\u5206\u4eab\u7ed9\u66f4\u591a\u7684\u9700\u8981\u7684\u670b\u53cb\u5b66\u4e60~<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u5982\u679c\u961f\u5217\u53ef\u4ee5\u4ece\u5904\u7406\u51fd\u6570\u672c\u8eab\u589e\u957f\uff0c\u6211\u8bd5\u56fe\u4e86\u89e3\u5982\u4f55\u5728 go \u4e2d\u5904\u7406\u961f\u5217\u3002\u8bf7\u53c2\u9605\u4e0b\u9762\u7684\u4ee3\u7801\u3002 <\/p>\n<p>\u5728\u6b64\u4f2a\u4ee3\u7801\u4e2d\uff0c\u6211\u60f3\u5c06\u6b63\u5728\u521b\u5efa\u7684\u5904\u7406\u7a0b\u5e8f\u6570\u91cf\u9650\u5236\u4e3a 10 \u4e2a\u3002\u56e0\u6b64\uff0c\u6211\u521b\u5efa\u4e86 10 \u4e2a\u5904\u7406\u961f\u5217\u7684\u5904\u7406\u7a0b\u5e8f\u3002\u7136\u540e\u6211\u7528\u4e00\u4e2a url \u5f00\u59cb\u961f\u5217\u3002 <\/p>\n<p>\u6211\u7684\u95ee\u9898\u662f\uff0c\u6839\u636e\u6587\u6863\uff0c\u901a\u9053\u7684 <code>sender<\/code> \u5c06\u963b\u585e\uff0c\u76f4\u5230\u63a5\u6536\u5668\u6536\u5230\u6570\u636e\u3002\u5728\u4e0b\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6bcf\u4e2a\u8fdb\u7a0b\u90fd\u662f\u4e00\u4e2a\u5904\u7406\u65b0 url \u7684\u63a5\u6536\u8005\u3002\u7136\u800c\uff0c\u5f88\u5bb9\u6613\u770b\u51fa\uff0c\u5982\u679c\u4e00\u4e2a\u8fdb\u7a0b\u5411\u961f\u5217\u53d1\u9001 11 \u4e2a\u94fe\u63a5\uff0c\u5b83\u5c06\u963b\u585e\uff0c\u76f4\u5230\u6240\u6709\u63a5\u6536\u8005\u5904\u7406\u5b8c\u8fd9\u4e9b\u65b0\u94fe\u63a5\u4e3a\u6b62\u3002\u5982\u679c\u8fd9\u4e9b\u63a5\u6536\u5668\u6bcf\u4e2a\u90fd\u6709 1 \u4e2a\u94fe\u63a5\uff0c\u90a3\u4e48\u5b83\u4eec\u5728\u5411\u961f\u5217\u53d1\u9001\u65b0\u7684 1 \u4e2a\u94fe\u63a5\u65f6\u4e5f\u4f1a\u963b\u585e\u3002\u7531\u4e8e\u6bcf\u4e2a\u4eba\u90fd\u88ab\u5c01\u9501\uff0c\u6240\u4ee5\u4ec0\u4e48\u90fd\u6ca1\u6709\u5b8c\u6210\u3002 <\/p>\n<p>\u6211\u60f3\u77e5\u9053 go \u4e2d\u7684\u4e00\u822c\u89e3\u51b3\u65b9\u6848\u662f\u4ec0\u4e48\uff0c\u7528\u4e8e\u5904\u7406\u53ef\u4ee5\u4ece\u8fdb\u7a0b\u672c\u8eab\u589e\u957f\u7684\u961f\u5217\u3002\u8bf7\u6ce8\u610f\uff0c\u6211\u8ba4\u4e3a\u6211\u53ef\u4ee5\u901a\u8fc7\u9501\u5b9a\u540d\u4e3a <code>queue<\/code> \u7684\u6570\u7ec4\u6765\u5b8c\u6210\u6b64\u64cd\u4f5c\uff0c\u4f46\u6211\u8bd5\u56fe\u4e86\u89e3\u5982\u4f55\u4f7f\u7528\u901a\u9053\u6765\u5b8c\u6210\u6b64\u64cd\u4f5c\u3002<\/p>\n<pre>var queue = make(chan string)\n\nfunc process(){\n    for currentURL := range queue {\n        links, _ := ... \/\/ some http call that gets links from a url\n        for _, link := links {\n            queue &lt;- link\n        }\n    }\n}\n\nfunc main () {\n   for i :=0; i &lt; 10; i++ {\n        go process()\n   }\n\n   queue &lt;- \"https:\/\/stackoverflow.com\"\n   ...\n   \/\/ block until receive some quit message\n   &lt;-quit \n}<\/pre>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u7684\u4e00\u4e2a\u7b80\u5355\u65b9\u6cd5\u662f\u5c06\u6dfb\u52a0\u901a\u9053\u94fe\u63a5\u7684\u4ee3\u7801\u79fb\u52a8\u5230\u5b83\u81ea\u5df1\u7684 go \u4f8b\u7a0b\u4e2d\u3002 \u8fd9\u6837\uff0c\u60a8\u7684\u4e3b\u8981\u5904\u7406\u53ef\u4ee5\u7ee7\u7eed\uff0c\u800c\u963b\u585e\u7684\u901a\u9053\u5199\u5165\u5219\u4f1a\u963b\u585e\u5355\u72ec\u7684 go \u4f8b\u7a0b\u3002<\/p>\n<pre>func process(){\n    for currenturl := range queue {\n        links, _ := ... \/\/ some http call that gets links from a url\n        for _, link := links {\n            l := link \/\/ this is important! ...\n            \/\/ the loop will re-set the value of link before the go routine is started\n\n            go func(l) {\n                queue &lt;- link \/\/ we'll be blocked here...\n                \/\/ but the \"parent\" routine can still iterate through the channel\n                \/\/ which in turn un-blocks the write\n            }(l)\n        }\n    }\n}<\/pre>\n<p>\u4f7f\u7528\u4fe1\u53f7\u91cf\u793a\u4f8b\u8fdb\u884c\u7f16\u8f91\u4ee5\u9650\u5236 go \u4f8b\u7a0b\uff1a<\/p>\n<pre>func main () {\n    maxworkers := 5000\n    sem := semaphore.newweighted(int64(maxworkers))\n    ctx := context.todo()\n    for i :=0; i &lt; 10; i++ {\n        go process(ctx)\n    }\n\n    queue &lt;- \"https:\/\/stackoverflow.com\"\n    \/\/ block until receive some quit message\n    &lt;-quit \n}\n\nfunc process(ctx context.context){\n    for currenturl := range queue {\n        links, _ := ... \/\/ some http call that gets links from a url\n        for _, link := links {\n            l := link \/\/ this is important! ...\n            \/\/ the loop will re-set the value of link before the go routine is started\n\n            \/\/ acquire a go routine...\n            \/\/ if we are at the routine limit, this line will block until one becomes available\n            sem.acquire(ctx, 1)\n            go func(l) {\n                defer sem.release(1)\n                queue &lt;- link \/\/ we'll be blocked here...\n                \/\/ but the \"parent\" routine can still iterate through the channel\n                \/\/ which in turn un-blocks the write\n            }(l)\n        }\n    }\n}<\/pre>\n<p>\u8fd9\u4e2a\u9009\u9879\u6700\u7ec8\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6b7b\u9501&#8230;\u5047\u8bbe\u6240\u6709 go \u4f8b\u7a0b\u90fd\u5df2\u88ab\u58f0\u660e\uff0c\u7236\u5faa\u73af\u53ef\u80fd\u4f1a\u88ab\u9501\u5b9a\u5728 <code>sem.acquire<\/code> \u4e0a\u3002\u8fd9\u5c06\u5bfc\u81f4\u5b50\u4f8b\u7a0b\u6c38\u8fdc\u4e0d\u4f1a\u6dfb\u52a0\u5230\u901a\u9053\uff0c\u56e0\u6b64\u6c38\u8fdc\u4e0d\u4f1a\u6267\u884c\u5ef6\u8fdf\u7684 <code>sem.release<\/code>\u3002\u6211\u6b63\u5728\u52aa\u529b\u60f3\u51fa\u4e00\u4e2a\u597d\u65b9\u6cd5\u6765\u5904\u7406\u8fd9\u4e2a\u95ee\u9898\u3002\u4e5f\u8bb8\u662f\u5916\u90e8\u5185\u5b58\u961f\u5217\u800c\u4e0d\u662f\u901a\u9053\uff1f<\/p>\n<p>\u60a8\u53ef\u4ee5\u505a\u4e24\u4ef6\u4e8b\uff0c\u6216\u8005\u4f7f\u7528\u7f13\u51b2\u901a\u9053\uff0c\u5373\u4f7f\u53e6\u4e00\u7aef\u6ca1\u6709\u4eba\u63a5\u6536\uff0c\u4e5f\u4e0d\u4f1a\u963b\u585e\u3002\u8fd9\u6837\u60a8\u5c31\u53ef\u4ee5\u7acb\u5373\u5237\u65b0\u901a\u9053\u5185\u7684\u503c\u3002<\/p>\n<p>\u4e00\u79cd\u66f4\u6709\u6548\u7684\u65b9\u6cd5\u662f\u68c0\u67e5\u901a\u9053\u4e2d\u662f\u5426\u6709\u4efb\u4f55\u53ef\u7528\u503c\u6216\u901a\u9053\u662f\u5426\u5df2\u5173\u95ed\uff0c\u8fd9\u5e94\u8be5\u5728\u53d1\u9001\u6240\u6709\u503c\u65f6\u7531\u53d1\u9001\u8005\u8fdb\u884c\u3002<\/p>\n<pre>v, ok := &lt;-ch<\/pre>\n<p>\u5982\u679c\u6ca1\u6709\u66f4\u591a\u503c\u8981\u63a5\u6536\u4e14\u901a\u9053\u5df2\u5173\u95ed\uff0c\u5219 <code>ok<\/code> \u4e3a <code>false<\/code>\u3002\u4f7f\u7528 select as \u68c0\u67e5\u901a\u9053\u5185\u7684\u503c<\/p>\n<pre>package main\n\nimport (\n    \"fmt\"\n    \"sync\"\n)\n\nvar queue = make(chan int)\nvar wg sync.WaitGroup\n\nfunc process(){\n        values := []int{1,2,5,3,9,7}\n        for _, value := range values {\n            queue &lt;- value        \n        }\n}\n\nfunc main () {\n   for i :=0; i &lt; 10; i++ {\n        go process()\n   }\n   wg.Add(1)\n   go func(){\n      defer wg.Done()\n      for j:=0;j&lt;30;j++ {\n          select {\n             case &lt;-queue:\n        fmt.Println(&lt;-queue)\n          } \n      }\n   }()\n   wg.Wait()\n   close(queue)\n}<\/pre>\n<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u300a\u5982\u4f55\u5904\u7406\u53ef\u4ee5\u589e\u957f\u800c\u4e0d\u4f1a\u963b\u585e\u7684\u961f\u5217\u300b\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u5173\u4e8e\u7684\u8d44\u6599\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-207297","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207297","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=207297"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207297\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207297"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207297"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207297"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}