如何实现基于行的文件内容的并行处理
本篇文章给大家分享《如何实现基于行的文件内容的并行处理》,覆盖了Golang的常见基础知识,其实一个语言的全部知识点一篇文章是不可能说完的,但希望通过这些问题,让读者对自己的掌握程度有一定的认识(B 数),从而弥补自己的不足,更好的掌握它。
问题内容
我正在编写一个 POC 来处理一个非常大的文本文件 ~10 亿多行,并为此尝试使用 Go;
package main import ( "bufio" "fmt" "log" "os" "time" ) func main() { start := time.Now() file, err := os.Open("dump10.txt") if err != nil { log.Fatal(err) } defer file.Close() scanner := bufio.NewScanner(file) for scanner.Scan() { go fmt.Println(scanner.Text()) } if err := scanner.Err(); err != nil { log.Fatal(err) } secs := time.Since(start).Seconds() fmt.Printf("Took %.2fs", secs) }
但是,在运行此程序时,我收到此错误;
恐慌:单个文件或套接字上的并发操作过多(最大 1048575)
我还没有在网上找到任何处理这个特定错误的东西。我不确定这是否是文件描述符问题,错误中列出的最大值远高于我ulimit -n
的 500,000 限制。
做这个的最好方式是什么?
由于它不是很明显,它fmt.Println
是我在处理数据时将调用的实际函数的替身。
正确答案
在考虑并行化进程之前,您应该研究您的输入和计算以确保它有意义。
需要按顺序处理的输入不是很好的匹配,因为并行处理需要额外的复杂指令来保持顺序,很难预先评估这种策略是否会成功。
此外,为了利用并行化,运行计算必须花费比同步并行任务所需的时间更长的时间。通过批量处理数据可能会超过此成本,但生成的算法将更加复杂,并会产生额外的不良副作用(如分配)。
否则,不要并行化。
请参阅以下具有长/短计算时间的各种实现示例及其结果基准。
结论是,除非您计算一个明显超过同步成本的长时间运行的异步任务,否则顺序处理会更快。
main.go
package main import ( "bufio" "fmt" "io" "runtime" "strings" "sync" "time" ) func main() { data := strings.Repeat(strings.Repeat("a", 1000)+"\n", 1000) run_line_short(data, true) run_line_long(data, true) run_line_short_workers(data, true) run_line_long_workers(data, true) run_bulk_short(data, true) run_bulk_long(data, true) run_seq_short(data, true) run_seq_long(data, true) } func run_line_short(data string, stat bool) { if stat { s := stats("run_line_short") defer s() } r := strings.NewReader(data) err := process(r, line_handler_short) if err != nil { panic(err) } } func run_line_long(data string, stat bool) { if stat { s := stats("run_line_long") defer s() } r := strings.NewReader(data) err := process(r, line_handler_long) if err != nil { panic(err) } } func run_line_short_workers(data string, stat bool) { if stat { s := stats("run_line_short_workers") defer s() } r := strings.NewReader(data) err := processWorkers(r, line_handler_short) if err != nil { panic(err) } } func run_line_long_workers(data string, stat bool) { if stat { s := stats("run_line_long_workers") defer s() } r := strings.NewReader(data) err := processWorkers(r, line_handler_long) if err != nil { panic(err) } } func run_bulk_short(data string, stat bool) { if stat { s := stats("run_bulk_short") defer s() } r := strings.NewReader(data) err := processBulk(r, bulk_handler_short) if err != nil { panic(err) } } func run_bulk_long(data string, stat bool) { if stat { s := stats("run_bulk_long") defer s() } r := strings.NewReader(data) err := processBulk(r, bulk_handler_long) if err != nil { panic(err) } } func run_seq_short(data string, stat bool) { if stat { s := stats("run_seq_short") defer s() } r := strings.NewReader(data) err := processSeq(r, line_handler_short) if err != nil { panic(err) } } func run_seq_long(data string, stat bool) { if stat { s := stats("run_seq_long") defer s() } r := strings.NewReader(data) err := processSeq(r, line_handler_long) if err != nil { panic(err) } } func line_handler_short(k string) error { _ = len(k) return nil } func line_handler_long(k string) error { 0 { inputmain_test.go
package main import ( "strings" "testing" ) func Benchmark_run_line_short(b *testing.B) { data := strings.Repeat(strings.Repeat("a", 1000)+"\n", 1000) for i := 0; i结果
$ go run main.go ====================== run_line_short time to run 2.747827ms Alloc: 2 MB, TotalAlloc: 2 MB, Sys: 68 MB Mallocs: 1378, Frees: 1 HeapAlloc: 2 MB, HeapSys: 63 MB, HeapIdle: 61 MB HeapObjects: 1377 ====================== run_line_long time to run 1.30987804s Alloc: 3 MB, TotalAlloc: 3 MB, Sys: 68 MB Mallocs: 5619, Frees: 5 HeapAlloc: 3 MB, HeapSys: 63 MB, HeapIdle: 59 MB HeapObjects: 5614 ====================== run_line_short_workers time to run 4.54926ms Alloc: 1 MB, TotalAlloc: 4 MB, Sys: 68 MB Mallocs: 6648, Frees: 5743 HeapAlloc: 1 MB, HeapSys: 63 MB, HeapIdle: 61 MB HeapObjects: 905 ====================== run_line_long_workers time to run 1.29874118s Alloc: 2 MB, TotalAlloc: 5 MB, Sys: 68 MB Mallocs: 10670, Frees: 5747 HeapAlloc: 2 MB, HeapSys: 63 MB, HeapIdle: 60 MB HeapObjects: 4923 ====================== run_bulk_short time to run 1.279059ms Alloc: 3 MB, TotalAlloc: 6 MB, Sys: 68 MB Mallocs: 11695, Frees: 5751 HeapAlloc: 3 MB, HeapSys: 63 MB, HeapIdle: 59 MB HeapObjects: 5944 ====================== run_bulk_long time to run 31.328652ms Alloc: 1 MB, TotalAlloc: 7 MB, Sys: 68 MB Mallocs: 12728, Frees: 11361 HeapAlloc: 1 MB, HeapSys: 63 MB, HeapIdle: 61 MB HeapObjects: 1367 ====================== run_seq_short time to run 956.991碌s Alloc: 3 MB, TotalAlloc: 8 MB, Sys: 68 MB Mallocs: 13746, Frees: 11160 HeapAlloc: 3 MB, HeapSys: 63 MB, HeapIdle: 59 MB HeapObjects: 2586 ====================== run_seq_long time to run 5.195705859s Alloc: 1 MB, TotalAlloc: 9 MB, Sys: 68 MB Mallocs: 17766, Frees: 15973 HeapAlloc: 1 MB, HeapSys: 63 MB, HeapIdle: 61 MB HeapObjects: 1793 [mh-cbon@Host-001 bulk] $ go test -bench=. -benchmem -count=4 goos: linux goarch: amd64 pkg: test/bulk Benchmark_run_line_short-4 1000 1750824 ns/op 1029354 B/op 1005 allocs/op Benchmark_run_line_short-4 1000 1747408 ns/op 1029348 B/op 1005 allocs/op Benchmark_run_line_short-4 1000 1757826 ns/op 1029352 B/op 1005 allocs/op Benchmark_run_line_short-4 1000 1758427 ns/op 1029352 B/op 1005 allocs/op Benchmark_run_line_long-4 1 1303037704 ns/op 2253776 B/op 4075 allocs/op Benchmark_run_line_long-4 1 1305074974 ns/op 2247792 B/op 4032 allocs/op Benchmark_run_line_long-4 1 1305353658 ns/op 2246320 B/op 4013 allocs/op Benchmark_run_line_long-4 1 1305725817 ns/op 2247792 B/op 4031 allocs/op Benchmark_run_line_short_workers-4 1000 2148354 ns/op 1029366 B/op 1005 allocs/op Benchmark_run_line_short_workers-4 1000 2139629 ns/op 1029370 B/op 1005 allocs/op Benchmark_run_line_short_workers-4 1000 1983352 ns/op 1029359 B/op 1005 allocs/op Benchmark_run_line_short_workers-4 1000 1909968 ns/op 1029363 B/op 1005 allocs/op Benchmark_run_line_long_workers-4 1 1298321093 ns/op 2247856 B/op 4013 allocs/op Benchmark_run_line_long_workers-4 1 1299846127 ns/op 2246384 B/op 4012 allocs/op Benchmark_run_line_long_workers-4 1 1300003625 ns/op 2246288 B/op 4011 allocs/op Benchmark_run_line_long_workers-4 1 1302779911 ns/op 2246256 B/op 4011 allocs/op Benchmark_run_bulk_short-4 2000 704358 ns/op 1082154 B/op 1011 allocs/op Benchmark_run_bulk_short-4 2000 708563 ns/op 1082147 B/op 1011 allocs/op Benchmark_run_bulk_short-4 2000 714687 ns/op 1082148 B/op 1011 allocs/op Benchmark_run_bulk_short-4 2000 705546 ns/op 1082156 B/op 1011 allocs/op Benchmark_run_bulk_long-4 50 31411412 ns/op 1051497 B/op 1088 allocs/op Benchmark_run_bulk_long-4 50 31513018 ns/op 1051544 B/op 1088 allocs/op Benchmark_run_bulk_long-4 50 31539311 ns/op 1051502 B/op 1088 allocs/op Benchmark_run_bulk_long-4 50 31564940 ns/op 1051505 B/op 1088 allocs/op Benchmark_run_seq_short-4 2000 574346 ns/op 1028632 B/op 1002 allocs/op Benchmark_run_seq_short-4 3000 572857 ns/op 1028464 B/op 1002 allocs/op Benchmark_run_seq_short-4 2000 580493 ns/op 1028632 B/op 1002 allocs/op Benchmark_run_seq_short-4 3000 572240 ns/op 1028464 B/op 1002 allocs/op Benchmark_run_seq_long-4 1 5196313302 ns/op 2245792 B/op 4005 allocs/op Benchmark_run_seq_long-4 1 5199995649 ns/op 2245792 B/op 4005 allocs/op Benchmark_run_seq_long-4 1 5200460425 ns/op 2245792 B/op 4005 allocs/op Benchmark_run_seq_long-4 1 5201080570 ns/op 2245792 B/op 4005 allocs/op PASS ok test/bulk 68.944s笔记:令我惊讶的
run_line_short_workers
是,它比 略慢run_line_short
,我没有解释这个结果,但是使用 pprof 进行更深入的分析应该可以提供答案。以上就是《如何实现基于行的文件内容的并行处理》的详细内容,更多关于golang的资料请关注golang学习网公众号!

- 上一篇
- 如何使用 mongo-go-driver 有效地将 bson 转换为 json?

- 下一篇
- 从通用且以某种方式动态的 go 地图获取内容的最佳方式是什么?
-
- 体贴的世界
- 赞 👍👍,一直没懂这个问题,但其实工作中常常有遇到...不过今天到这,看完之后很有帮助,总算是懂了,感谢老哥分享技术贴!
- 2023-06-19 13:09:26
-
- 高兴的黄蜂
- 好细啊,已加入收藏夹了,感谢老哥的这篇文章,我会继续支持!
- 2023-05-21 03:09:38
-
- 标致的冬日
- 这篇博文出现的刚刚好,好细啊,很好,码住,关注老哥了!希望老哥能多写Golang相关的文章。
- 2023-04-30 16:00:36
-
- Golang · Go问答 | 1年前 |
- 在读取缓冲通道中的内容之前退出
- 139浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 戈兰岛的全球 GOPRIVATE 设置
- 204浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将结构作为参数传递给 xml-rpc
- 325浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何用golang获得小数点以下两位长度?
- 477浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何通过 client-go 和 golang 检索 Kubernetes 指标
- 486浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将多个“参数”映射到单个可变参数的习惯用法
- 439浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 将 HTTP 响应正文写入文件后出现 EOF 错误
- 357浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 结构中映射的匿名列表的“复合文字中缺少类型”
- 352浏览 收藏
-
- Golang · Go问答 | 1年前 |
- NATS Jetstream 的性能
- 101浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何将复杂的字符串输入转换为mapstring?
- 440浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 相当于GoLang中Java将Object作为方法参数传递
- 212浏览 收藏
-
- Golang · Go问答 | 1年前 |
- 如何确保所有 goroutine 在没有 time.Sleep 的情况下终止?
- 143浏览 收藏
-
- 前端进阶之JavaScript设计模式
- 设计模式是开发人员在软件开发过程中面临一般问题时的解决方案,代表了最佳的实践。本课程的主打内容包括JS常见设计模式以及具体应用场景,打造一站式知识长龙服务,适合有JS基础的同学学习。
- 542次学习
-
- GO语言核心编程课程
- 本课程采用真实案例,全面具体可落地,从理论到实践,一步一步将GO核心编程技术、编程思想、底层实现融会贯通,使学习者贴近时代脉搏,做IT互联网时代的弄潮儿。
- 508次学习
-
- 简单聊聊mysql8与网络通信
- 如有问题加微信:Le-studyg;在课程中,我们将首先介绍MySQL8的新特性,包括性能优化、安全增强、新数据类型等,帮助学生快速熟悉MySQL8的最新功能。接着,我们将深入解析MySQL的网络通信机制,包括协议、连接管理、数据传输等,让
- 497次学习
-
- JavaScript正则表达式基础与实战
- 在任何一门编程语言中,正则表达式,都是一项重要的知识,它提供了高效的字符串匹配与捕获机制,可以极大的简化程序设计。
- 487次学习
-
- 从零制作响应式网站—Grid布局
- 本系列教程将展示从零制作一个假想的网络科技公司官网,分为导航,轮播,关于我们,成功案例,服务流程,团队介绍,数据部分,公司动态,底部信息等内容区块。网站整体采用CSSGrid布局,支持响应式,有流畅过渡和展现动画。
- 484次学习
-
- 笔灵AI生成答辩PPT
- 探索笔灵AI生成答辩PPT的强大功能,快速制作高质量答辩PPT。精准内容提取、多样模板匹配、数据可视化、配套自述稿生成,让您的学术和职场展示更加专业与高效。
- 24次使用
-
- 知网AIGC检测服务系统
- 知网AIGC检测服务系统,专注于检测学术文本中的疑似AI生成内容。依托知网海量高质量文献资源,结合先进的“知识增强AIGC检测技术”,系统能够从语言模式和语义逻辑两方面精准识别AI生成内容,适用于学术研究、教育和企业领域,确保文本的真实性和原创性。
- 38次使用
-
- AIGC检测-Aibiye
- AIbiye官网推出的AIGC检测服务,专注于检测ChatGPT、Gemini、Claude等AIGC工具生成的文本,帮助用户确保论文的原创性和学术规范。支持txt和doc(x)格式,检测范围为论文正文,提供高准确性和便捷的用户体验。
- 37次使用
-
- 易笔AI论文
- 易笔AI论文平台提供自动写作、格式校对、查重检测等功能,支持多种学术领域的论文生成。价格优惠,界面友好,操作简便,适用于学术研究者、学生及论文辅导机构。
- 48次使用
-
- 笔启AI论文写作平台
- 笔启AI论文写作平台提供多类型论文生成服务,支持多语言写作,满足学术研究者、学生和职场人士的需求。平台采用AI 4.0版本,确保论文质量和原创性,并提供查重保障和隐私保护。
- 41次使用
-
- 老师代码没有自动跟踪?
- 2023-03-07 439浏览
-
- c程序fork并等待golang进程状态
- 2023-03-05 262浏览
-
- GOLANG使用Context管理关联goroutine的方法
- 2022-12-28 193浏览
-
- 怎么用Golang将MySQL表转储为JSON
- 2023-03-07 188浏览
-
- Golang 检查字符串是否为有效路径?
- 2023-03-10 500浏览