{"id":262891,"date":"2023-02-28T08:59:59","date_gmt":"2023-02-28T00:59:59","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=262891"},"modified":"2023-01-30T12:00:48","modified_gmt":"2023-01-30T04:00:48","slug":"append-linux-goroutine","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/append-linux-goroutine.html","title":{"rendered":"go\u9ad8\u5e76\u53d1\u65f6append\u65b9\u6cd5\u5076\u73b0\u9519\u8bef\u89e3\u6790"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4e3a\u5927\u5bb6\u4ecb\u7ecd\u4e86go\u9ad8\u5e76\u53d1\u65f6append\u65b9\u6cd5\u5076\u73b0\u9519\u8bef\u89e3\u51b3\u5206\u6790\uff0c\u6709\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u501f\u9274\u53c2\u8003\u4e0b\uff0c\u5e0c\u671b\u80fd\u591f\u6709\u6240\u5e2e\u52a9\uff0c\u795d\u5927\u5bb6\u591a\u591a\u8fdb\u6b65\uff0c\u65e9\u65e5\u5347\u804c\u52a0\u85aa<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u80cc\u666f<\/strong><\/div>\n

\u5728\u5b9e\u73b0\u56fe\u7247\u8f6c\u7801\u7684\u9700\u6c42\u65f6\uff0c\u9700\u8981\u652f\u6301\u6700\u5927 500 \u4e2a\u56fe\u7247\u4e0b\u8f7d\u540e\u8f6c\u6362\u683c\u5f0f\uff1b<\/p>\n

\u5982\u679c\u662f\u4e00\u4e2a\u4e00\u4e2a\u4e0b\u8f7d\u540e\u8f6c\u7801\uff0c\u8017\u65f6\u592a\u957f\uff0c\u9700\u8981\u4f7f\u7528 goroutine \u5b9e\u73b0 500 \u4e2a\u56fe\u7247\u5e76\u53d1\u4e0b\u8f7d\u540e\uff0c\u5e76\u53d1\u8f6c\u7801\uff1b<\/p>\n

\u4f46\u81ea\u6d4b\u8fc7\u7a0b\u4e2d\u53d1\u73b0,\u4f1a\u5076\u73b0\u4e0b\u8f7d\u540e\u53ea\u8f6c\u6362\u4e86 499 \u4e2a\u56fe\u7247\u6216\u66f4\u5c11\u7684\u60c5\u51b5\uff08\u5168\u90e8\u4e0b\u8f7d\u3001\u8f6c\u7801\u6210\u529f\u7684\u6761\u4ef6\u4e0b\uff09\uff1b<\/p>\n

\u7136\u540e\u5c31\u5f00\u59cb\u4e86\u6253\u5370\u65e5\u5fd7\u627e bug \u7684\u8fc7\u7a0b\u3002<\/p>\n

\u6392\u67e5\u95ee\u9898<\/strong><\/div>\n

\u56e0\u4e3a\u5e76\u53d1\u65f6\u4f7f\u7528\u5230\u4e86 sync \u7b49\u5f85\u5168\u90e8\u534f\u7a0b\u7ed3\u675f\uff0c\u8d77\u521d\u4ee5\u4e3a\u662f sync \u5f02\u6b65\u7b49\u5f85\u51fa\u4e86\u95ee\u9898\uff1b<\/p>\n

\u6253\u5370\u65e5\u5fd7\u53d1\u73b0\uff0c\u6b63\u5e38\u6267\u884c\u4e86 500 \u6b21\u4e0b\u8f7d\uff0c\u6267\u884c\u5b8c\u6210\u4e0b\u8f7d\u4e4b\u540e\uff0c\u7ee7\u7eed\u6267\u884c\u7684\u8f6c\u7801\u64cd\u4f5c\uff0c\u6392\u9664 sync \u5f02\u6b65\u7b49\u5f85\u6709\u95ee\u9898\uff1b<\/p>\n

\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

import (\r\n    \"github.com\/satori\/go.uuid\"\r\n    \"sync\"\r\n)\r\nfunc downloadFiles(nWait *sync.WaitGroup, urls []interface{}, successFiles *[]string, failedFiles *[]string) {\r\n    \/\/ \u904d\u5386 urls \u8fdb\u884c\u4e0b\u8f7d\r\n    for _, value := range urls {\r\n        go func(value interface{}) {\r\n            defer nWait.Done()                                                     \/\/ \u6267\u884c\u7ed3\u675f\uff0c\u534f\u7a0b\u51cf 1\r\n            fullname := config.TranscodeDownloadPath + \"\/\" + uuid.NewV4().String() \/\/ \u9700\u8981\u786e\u4fdd\u6587\u4ef6\u540d\u7684\u552f\u4e00\u6027 (\u9632\u6b62\u4e0d\u540c\u7528\u6237\u540c\u4e00\u65f6\u95f4\u64cd\u4f5c\u4e86\u540c\u4e00\u6587\u4ef6\uff0c\u5bfc\u81f4\u8f6c\u7801\u5931\u8d25)\r\n            err := utils.DownloadCeph(value.(string), fullname)                    \/\/ \u4e0b\u8f7d\u6587\u4ef6\r\n            \/\/ \u4e0b\u8f7d\u6587\u4ef6\u72b6\u6001\u8bb0\u5f55\r\n            if err != nil {\r\n                *failedFiles = append(*failedFiles, fullname)\r\n            } else {\r\n                *successFiles = append(*successFiles, fullname)\r\n            }\r\n        }(value)\r\n    }\r\n}\r\n\/\/ \u524d\u7aef\u4f20\u5165\u7684\u56fe\u7247 url\r\nstrUrlList := req[\"strUrlList\"]\r\n\/\/ \u521d\u59cb\u5316\u53d8\u91cf\r\nnWait := sync.WaitGroup{}          \/\/ \u591a\u534f\u7a0b\u5f02\u6b65\u7b49\u5f85\r\nvar successFiles []string  \/\/ \u4e0b\u8f7d\u6210\u529f\u6587\u4ef6\r\nvar failedFiles []string           \/\/ \u4e0b\u8f7d\u5931\u8d25\u6587\u4ef6\r\n\/\/ \u904d\u5386 strUrlList \u8fdb\u884c\u4e0b\u8f7d\r\nlog.Error(\"\u5f00\u59cb\u4e0b\u8f7d\uff01\u957f\u5ea6\uff1a\", len(strUrlList))\r\nnWait.Add(len(strUrlList)) \/\/ \u7b49\u5f85\u534f\u7a0b\u6570\r\ndownloadFiles(&nWait, strUrlList, &successFiles, &failedFiles)\r\nnWait.Wait() \/\/ \u963b\u585e\uff0c\u7b49\u5f85\u5b8c\u6210\r\nlog.Error(\"\u4e0b\u8f7d\u7ed3\u675f\uff01\u957f\u5ea6\uff1a\", len(successFiles))\r\n\/\/...\r\nlog.Error(\"\u4e0b\u8f7d\u8f6c\u7801\uff01\")\r\n\/\/...<\/pre>\n

\u65e5\u5fd7\u5982\u4e0b\uff1a<\/p>\n

2022-10-29 21:28:51.996 ERROR   services\/tools.go:149   \u5f00\u59cb\u4e0b\u8f7d\uff01\u957f\u5ea6\uff1a500\r\n2022-10-29 21:28:52.486 ERROR   services\/tools.go:153   \u4e0b\u8f7d\u7ed3\u675f\uff01\u957f\u5ea6\uff1a499\r\n2022-10-29 21:28:52.486 ERROR   services\/tools.go:155   \u5f00\u59cb\u8f6c\u7801\uff01<\/pre>\n

\u6253\u5370\u66f4\u8be6\u7ec6\u7684\u65e5\u5fd7\uff0c\u5bf9 for range \u5faa\u73af\u5185\u7684\u903b\u8f91\u8fdb\u884c\u6392\u67e5\uff1b<\/p>\n

\u5728\u5355\u4e2a for \u5faa\u73af\u7ed3\u675f\u65f6\u589e\u52a0\u65e5\u5fd7\uff1a<\/p>\n

log.Error(\"\u4e0b\u8f7d\u534f\u7a0b\u7ed3\u675f: \", len(*successFiles))<\/pre>\n

\u53d1\u73b0\u4e00\u5904\u7279\u6b8a\u7684\u65e5\u5fd7\uff1a<\/p>\n

2022-10-29 21:40:38.407 ERROR   services\/tools.go:35    \u4e0b\u8f7d\u534f\u7a0b\u7ed3\u675f: 63\r\n2022-10-29 21:40:38.407 ERROR   services\/tools.go:35    \u4e0b\u8f7d\u534f\u7a0b\u7ed3\u675f: 64\r\n2022-10-29 21:40:38.407 ERROR   services\/tools.go:35    \u4e0b\u8f7d\u534f\u7a0b\u7ed3\u675f: 65\r\n2022-10-29 21:40:38.407 ERROR   services\/tools.go:35    \u4e0b\u8f7d\u534f\u7a0b\u7ed3\u675f: 65\r\n2022-10-29 21:40:38.408 ERROR   services\/tools.go:35    \u4e0b\u8f7d\u534f\u7a0b\u7ed3\u675f: 66\r\n2022-10-29 21:40:38.408 ERROR   services\/tools.go:35    \u4e0b\u8f7d\u534f\u7a0b\u7ed3\u675f: 67\r\n<\/pre>\n

\u4e24\u6b21\u957f\u5ea6\u90fd\u662f 65\uff0c\u5207\u7247\u957f\u5ea6\u6ca1\u6709\u53d1\u751f\u53d8\u5316\uff0c\u540c\u4e00\u65f6\u95f4\u70b9\u6267\u884c\u4e24\u6b21\u5207\u7247 append \u65b9\u6cd5\uff0c\u4f1a\u5076\u73b0\u4e00\u6b21\u5931\u6548\uff0c\u95ee\u9898\u539f\u56e0\u627e\u5230\uff1b<\/p>\n

\u89e3\u51b3\u95ee\u9898<\/strong><\/div>\n

\u4f7f\u7528\u5207\u7247\u7d22\u5f15\u8fdb\u884c\u8d4b\u503c\uff0c\u4e0d\u518d\u4f7f\u7528 append \uff1b<\/p>\n

\u4fee\u590d\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n

import (\r\n    \"github.com\/satori\/go.uuid\"\r\n    \"sync\"\r\n)\r\nfunc downloadFiles(nWait *sync.WaitGroup, urls []interface{}, successFiles *[]string, failedFiles *[]string) {\r\n    \/\/ \u904d\u5386 urls \u8fdb\u884c\u4e0b\u8f7d\r\n    for index, value := range urls {\r\n        go func(index int, value interface{}) {\r\n            defer nWait.Done()                                                     \/\/ \u6267\u884c\u7ed3\u675f\uff0c\u534f\u7a0b\u51cf 1\r\n            fullname := config.TranscodeDownloadPath + \"\/\" + uuid.NewV4().String() \/\/ \u9700\u8981\u786e\u4fdd\u6587\u4ef6\u540d\u7684\u552f\u4e00\u6027 (\u9632\u6b62\u4e0d\u540c\u7528\u6237\u540c\u4e00\u65f6\u95f4\u64cd\u4f5c\u4e86\u540c\u4e00\u6587\u4ef6\uff0c\u5bfc\u81f4\u8f6c\u7801\u5931\u8d25)\r\n            err := utils.DownloadCeph(value.(string), fullname)                    \/\/ \u4e0b\u8f7d\u6587\u4ef6\r\n            \/\/ \u4e0b\u8f7d\u6587\u4ef6\u72b6\u6001\u8bb0\u5f55\r\n            if err != nil {\r\n                (*failedFiles)[index] = fullname\r\n            } else {\r\n                (*successFiles)[index] = fullname\r\n            }\r\n        }(index, value)\r\n    }\r\n}\r\n\/\/ \u524d\u7aef\u4f20\u5165\u7684\u56fe\u7247 url\r\nstrUrlList := req[\"strUrlList\"]\r\n\/\/ \u521d\u59cb\u5316\u53d8\u91cf\r\nnWait := sync.WaitGroup{}                                        \/\/ \u591a\u534f\u7a0b\u5f02\u6b65\u7b49\u5f85\r\nsuccessFiles := make([]string, len(strUrlList), len(strUrlList)) \/\/ \u4e0b\u8f7d\u6210\u529f\u6587\u4ef6\r\nfailedFiles := make([]string, len(strUrlList), len(strUrlList))  \/\/ \u4e0b\u8f7d\u5931\u8d25\u6587\u4ef6\r\n\/\/ \u904d\u5386 strUrlList \u8fdb\u884c\u4e0b\u8f7d\r\nnWait.Add(len(strUrlList)) \/\/ \u7b49\u5f85\u534f\u7a0b\u6570\r\ndownloadFiles(&nWait, strUrlList, &successFiles, &failedFiles)\r\nnWait.Wait() \/\/ \u963b\u585e\uff0c\u7b49\u5f85\u5b8c\u6210<\/pre>\n

\u4ee5\u4e0a\u5c31\u662fgo\u9ad8\u5e76\u53d1\u65f6append\u65b9\u6cd5\u5076\u73b0\u9519\u8bef\u89e3\u51b3\u5206\u6790\u7684\u8be6\u7ec6\u5185\u5bb9<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5728\u5b9e\u73b0\u56fe\u7247\u8f6c\u7801\u7684\u9700\u6c42\u65f6\uff0c\u9700\u8981\u652f\u6301\u6700\u5927 500 \u4e2a\u56fe\u7247\u4e0b\u8f7d\u540e\u8f6c\u6362\u683c\u5f0f\uff1b \u5982\u679c\u662f\u4e00\u4e2a\u4e00\u4e2a\u4e0b\u8f7d\u540e\u8f6c\u7801\uff0c\u8017\u65f6\u592a\u957f\uff0c\u9700\u8981 […]<\/p>\n","protected":false},"author":668,"featured_media":216259,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-262891","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262891","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/668"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=262891"}],"version-history":[{"count":2,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262891\/revisions"}],"predecessor-version":[{"id":262893,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/262891\/revisions\/262893"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/216259"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=262891"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=262891"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=262891"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}