{"id":207314,"date":"2025-07-08T10:14:00","date_gmt":"2025-07-08T02:14:00","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207314\/"},"modified":"2025-07-08T10:14:00","modified_gmt":"2025-07-08T02:14:00","slug":"%e4%b8%ba%e4%bb%80%e4%b9%88-goroutines-%e4%b8%ad%e7%9a%84-io-%e6%93%8d%e4%bd%9c%e8%be%83%e6%85%a2%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207314\/","title":{"rendered":"\u4e3a\u4ec0\u4e48 Goroutines \u4e2d\u7684 IO \u64cd\u4f5c\u8f83\u6162\uff1f"},"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>\u4e3a\u4ec0\u4e48 Goroutines \u4e2d\u7684 IO \u64cd\u4f5c\u8f83\u6162\uff1f<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-21 17:33:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2aGolang\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u4e3a\u4ec0\u4e48 Goroutines \u4e2d\u7684 IO \u64cd\u4f5c\u8f83\u6162\uff1f\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u4ece goroutine \u4e0b\u8f7d\u56fe\u50cf\u540e\uff0c\u6211\u6b63\u5728\u5904\u7406 io\u3002<\/p>\n<p>\u6d4b\u8bd5\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n<p>\u5728goroutine\u4e2d\u4e0b\u8f7d\u56fe\u50cf\u540e\uff0c\u6211\u53d1\u73b0io\u64cd\u4f5c\u975e\u5e38\u6162\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u76f8\u53cd\uff0c\u5b83\u4f1a\u5728 goroutine \u4e2d\u4e0b\u8f7d\u56fe\u50cf groutine \u9886\u57df\u4e4b\u5916\u7684 io \u64cd\u4f5c\u901f\u5ea6\u66f4\u5feb\u3002<\/p>\n<p>\u6211\u53ef\u4ee5\u77e5\u9053\u4e3a\u4ec0\u4e48\u5417\uff1f<\/p>\n<p>\u4e0b\u9762\u662f\u6d4b\u8bd5\u6e90\u7801<\/p>\n<pre>type imageresult struct {\n    targetimagepath string\n    success         bool\n}\n\ntype imagedownloadresult struct {\n    targetimagepath string\n    response        *http.response\n    success         bool\n}\n\nfunc main() {\n    downloadurls := []string{\n        \"https:\/\/myhost.com\/item\/image\/path\/name_01.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_02.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_03.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_05.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_07.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_08.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_09.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_10.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_11.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_12.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_13.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_14.png\",\n        \"https:\/\/myhost.com\/item\/image\/path\/name_16.png\",\n    }\n\n    startasyncio := time.now()\n    resultchannel := make(chan imageresult)\n    for _, downloadurl := range downloadurls {\n        go httpfiledownloadandiowithasync(downloadurl, resultchannel)\n    }\n\n    for i := 0; i &lt; len(downloadurls); i++ {\n        &lt;-resultchannel\n    }\n    fmt.println(\"total time async io: \", time.now().sub(startasyncio))\n\n    fmt.println(\"=======================vs========================\")\n\n    startsyncio := time.now()\n    resultchannel2 := make(chan imagedownloadresult)\n    for _, downloadurl := range downloadurls {\n        go httpfiledownloadwithasync(downloadurl, resultchannel2)\n\n    }\n\n    for i := 0; i &lt; len(downloadurls); i++ {\n        result := &lt;-resultchannel2\n        getbytesfromdownloaddata(result.response, result.targetimagepath)\n    }\n    fmt.println(\"total time sync io: \", time.now().sub(startsyncio))\n}<\/pre>\n<pre>func httpfiledownloadandiowithasync(downloadurl string, imageresult chan imageresult) {\n    result := imageresult{\n        targetimagepath: downloadurl,\n    }\n\n    client := http.client{\n        checkredirect: func(r *http.request, via []*http.request) error {\n            r.url.opaque = r.url.path\n            return nil\n        },\n    }\n    \/\/ put content on file\n    downstart := time.now()\n    resp, _ := client.get(downloadurl)\n    downend := time.now()\n    fmt.println(downloadurl, \"file download time : \", downend.sub(downstart))\n\n    defer resp.body.close()\n\n    \/\/ file read\n    iostart := time.now()\n    var buf bytes.buffer\n    io.copy(&amp;buf, resp.body)\n    ioend := time.now()\n    fmt.println(downloadurl, \"io time async : \", ioend.sub(iostart))\n\n    result.success = true\n    imageresult &lt;- result\n\n}\n\nfunc httpfiledownloadwithasync(downloadurl string, imageresult chan imagedownloadresult) {\n    result := imagedownloadresult{\n        targetimagepath: downloadurl,\n    }\n\n    client := http.client{\n        checkredirect: func(r *http.request, via []*http.request) error {\n            r.url.opaque = r.url.path\n            return nil\n        },\n    }\n    \/\/ put content on file\n    downstart := time.now()\n    resp, _ := client.get(downloadurl)\n    downend := time.now()\n    fmt.println(downloadurl, \"file download time : \", downend.sub(downstart))\n\n    result.success = true\n    result.response = resp\n    imageresult &lt;- result\n\n}\n\nfunc getbytesfromdownloaddata(resp *http.response, downloadurl string) []byte {\n    defer func() {\n        if err2 := resp.body.close(); err2 != nil {\n            fmt.println(\"fail download by image download close error:\", downloadurl)\n        }\n    }()\n    \/\/ file read\n    starttime := time.now()\n    var buf bytes.buffer\n    _, err := io.copy(&amp;buf, resp.body)\n\n    if err != nil {\n        fmt.println(\"fail download by read response body:\", downloadurl)\n        return nil\n    }\n    fmt.println(downloadurl, \"io time sync:\", time.now().sub(starttime))\n    return buf.bytes()\n}<\/pre>\n<p>\u4e0b\u9762\u662f\u65e5\u5fd7\u3002<\/p>\n<pre>https:\/\/myhost.com\/item\/image\/path\/name_13.png File Download Time :  197.058394ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_12.png File Download Time :  399.633804ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_08.png File Download Time :  587.309339ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_08.png IO Time Async :  314.482233ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_03.png File Download Time :  901.985524ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_05.png File Download Time :  1.132634351s\nhttps:\/\/myhost.com\/item\/image\/path\/name_02.png File Download Time :  1.132661015s\nhttps:\/\/myhost.com\/item\/image\/path\/name_14.png File Download Time :  1.132605289s\nhttps:\/\/myhost.com\/item\/image\/path\/name_09.png File Download Time :  1.132608987s\nhttps:\/\/myhost.com\/item\/image\/path\/name_16.png File Download Time :  1.133075291s\nhttps:\/\/myhost.com\/item\/image\/path\/name_01.png File Download Time :  1.132837045s\nhttps:\/\/myhost.com\/item\/image\/path\/name_11.png File Download Time :  1.133100234s\nhttps:\/\/myhost.com\/item\/image\/path\/name_10.png File Download Time :  1.132982295s\nhttps:\/\/myhost.com\/item\/image\/path\/name_07.png File Download Time :  1.133150493s\nhttps:\/\/myhost.com\/item\/image\/path\/name_12.png IO Time Async :  1.240533838s\nhttps:\/\/myhost.com\/item\/image\/path\/name_09.png IO Time Async :  849.335303ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_03.png IO Time Async :  1.080254194s\nhttps:\/\/myhost.com\/item\/image\/path\/name_02.png IO Time Async :  849.395964ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_13.png IO Time Async :  1.784857595s\nhttps:\/\/myhost.com\/item\/image\/path\/name_14.png IO Time Async :  849.642554ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_16.png IO Time Async :  849.494898ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_01.png IO Time Async :  850.297187ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_10.png IO Time Async :  864.482359ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_11.png IO Time Async :  864.524354ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_07.png IO Time Async :  874.676604ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_05.png IO Time Async :  875.22765ms\nTotal Time Async IO:  2.008162313s\n=======================VS========================\nhttps:\/\/myhost.com\/item\/image\/path\/name_09.png File Download Time :  72.476375ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_05.png File Download Time :  73.351299ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_07.png File Download Time :  92.839309ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_10.png File Download Time :  105.41514ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_08.png File Download Time :  136.861107ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_01.png File Download Time :  137.531384ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_16.png File Download Time :  204.833342ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_11.png File Download Time :  225.73164ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_03.png File Download Time :  238.569755ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_09.png IO Time Sync: 251.986344ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_14.png File Download Time :  473.071003ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_02.png File Download Time :  523.402477ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_13.png File Download Time :  523.389256ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_12.png File Download Time :  523.412647ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_05.png IO Time Sync: 549.364233ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_07.png IO Time Sync: 890.004\u00b5s\nhttps:\/\/myhost.com\/item\/image\/path\/name_10.png IO Time Sync: 545.761\u00b5s\nhttps:\/\/myhost.com\/item\/image\/path\/name_08.png IO Time Sync: 229.321\u00b5s\nhttps:\/\/myhost.com\/item\/image\/path\/name_01.png IO Time Sync: 601.996\u00b5s\nhttps:\/\/myhost.com\/item\/image\/path\/name_16.png IO Time Sync: 12.912227ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_11.png IO Time Sync: 148.432703ms\nhttps:\/\/myhost.com\/item\/image\/path\/name_03.png IO Time Sync: 336.862\u00b5s\nhttps:\/\/myhost.com\/item\/image\/path\/name_14.png IO Time Sync: 239.328\u00b5s\nhttps:\/\/myhost.com\/item\/image\/path\/name_02.png IO Time Sync: 483.976\u00b5s\nhttps:\/\/myhost.com\/item\/image\/path\/name_13.png IO Time Sync: 215.655\u00b5s\nhttps:\/\/myhost.com\/item\/image\/path\/name_12.png IO Time Sync: 265.376\u00b5s\nTotal Time Sync IO:  1.039298797s<\/pre>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>Go \u4ee3\u7801\u603b\u662f\u5728 goroutine \u4e2d\u6267\u884c\u3002<\/p>\n<p>Goroutine \u662f\u5e73\u7b49\u7684\uff0c\u4e0d\u533a\u5206\uff08\u9664\u975e\u8fd0\u884c <code>main()<\/code> \u51fd\u6570\u7684 <code>main()<\/code> goroutine \u7ed3\u675f\u65f6\uff0c\u6574\u4e2a\u5e94\u7528\u7a0b\u5e8f\u7ec8\u6b62\uff09\u3002<\/p>\n<p>Goroutines \u88ab\u8c03\u5ea6\/\u590d\u7528\u5230\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\u4e0a\uff0c\u7ebf\u7a0b\u5728\u7269\u7406\u6216\u865a\u62df CPU \u6838\u5fc3\u4e0a\u8fd0\u884c\u3002\u56e0\u6b64\uff0c\u4e00\u4e2a Goroutine \u662f\u5426\u6bd4\u53e6\u4e00\u4e2a Goroutine \u8fd0\u884c\u5f97\u66f4\u6162\/\u66f4\u5feb\u53d6\u51b3\u4e8e\u6267\u884c\u5176\u6307\u4ee4\u7684 CPU \u6838\u5fc3\u7684\u52a0\u8f7d\u65b9\u5f0f\uff08\u7cfb\u7edf\u65b9\u9762\uff09\u3002\u6700\u7ec8\u6267\u884c\u4e00\u4e2a Goroutine \u6307\u4ee4\u7684 CPU \u6838\u5fc3\u53ef\u80fd\u4f1a\u6bd4\u53e6\u4e00\u4e2a Goroutine \u6307\u4ee4\u88ab\u66f4\u591a\u5730\u5229\u7528\uff0c\u4ece\u800c\u5bfc\u81f4\u611f\u77e5\u5230\u7684 Goroutine \u6027\u80fd\u66f4\u5dee\uff0c\u4f46\u8fd9\u5e76\u4e0d\u662f\u56e0\u4e3a Go \u7684\u8fd0\u884c\u65f6\u548c Goroutine \u8c03\u5ea6\u3002<\/p>\n<p>\u8bf7\u6ce8\u610f\uff0c\u53ef\u4ee5\u4f7f\u7528 \u5c06 Goroutine \u9501\u5b9a\u5230\u64cd\u4f5c\u7cfb\u7edf\u7ebf\u7a0b\uff0c\u8fd9\u610f\u5473\u7740 Goroutine \u5c06\u201c\u62e5\u6709\u201d\u8be5\u7ebf\u7a0b\uff08\u4e0d\u4f1a\u5c06\u5176\u4ed6 Goroutine \u8c03\u5ea6\u5230\u8be5\u7ebf\u7a0b\u4e0a\uff09\uff0c\u4f46\u60a8\u4e0d\u4f1a\u5728\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u5b83\u3002 <\/p>\n<p>\u56e0\u6b64\uff0c\u60a8\u4ece\u5176\u4ed6 goroutines \u9047\u5230\u7684\u4e0b\u8f7d\u901f\u5ea6\u8f83\u6162\u7684\u60c5\u51b5\u4e0e Go \u65e0\u5173\uff0c\u5b83\u53ef\u80fd\u4e0e\u60a8\u7684\u64cd\u4f5c\u7cfb\u7edf\u548c CPU \u8d1f\u8f7d\u6216\u60a8\u8c03\u7528\u7684\u5916\u90e8\u670d\u52a1\uff08HTTP \u670d\u52a1\u5668\uff09\u6709\u5173\u3002<\/p>\n<p>\u53e6\u8bf7\u6ce8\u610f\uff0c\u518d\u6b21\u8fd0\u884c\u76f8\u540c\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u82b1\u8d39\u66f4\u5c11\u7684\u65f6\u95f4\uff0c\u521d\u59cb\u5316\u7684\u4ee3\u7801\u53ef\u80fd\u4f1a\u88ab\u91cd\u7528\uff0c\u5e76\u4e14\u8fde\u63a5\u5230\u540c\u4e00\u4e3b\u673a\u4e5f\u53ef\u80fd\u4f1a\u660e\u663e\u66f4\u5feb\uff1aDNS \u67e5\u627e\u4f1a\u88ab\u7f13\u5b58\uff0c\u751a\u81f3 TCP \u8fde\u63a5\u4e5f\u53ef\u80fd\u4f1a\u88ab\u7f13\u5b58\/\u6c60\u5316\u3002\u60a8\u8c03\u7528\u7684\u670d\u52a1\u5668\u4e5f\u53ef\u80fd\u7f13\u5b58\u67d0\u4e9b\u6570\u636e\uff0c\u56e0\u6b64\u83b7\u53d6\u76f8\u540c\u7684 URL \u4e5f\u53ef\u80fd\u4f1a\u660e\u663e\u66f4\u5feb\uff08\u5728\u540e\u7eed\u8c03\u7528\u4e2d\u4ece\u540c\u4e00\u670d\u52a1\u5668\u83b7\u53d6\u4e0d\u540c\u7684\u8d44\u6e90\u4e5f\u53ef\u80fd\u4f1a\u66f4\u5feb\uff0c\u67d0\u4e9b\u68c0\u67e5\uff08\u4f8b\u5982\u8eab\u4efd\u9a8c\u8bc1\/\u6388\u6743\uff09\u53ef\u80fd\u4f1a\u88ab\u7f13\u5b58\uff09\u3002<\/p>\n<p>\u67e5\u770b\u76f8\u5173\uff1a<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8eGolang\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/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-207314","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207314","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=207314"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207314\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}