{"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":"
\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 |