我在Swift3上有一个控制台应用程序,从非常大的file.txt(~200GB)逐行读取:
guard let reader = LineReader(path: "/Path/to/file.txt") else { return; }
for line in reader {
// do something with each line
}
从文件中读取所有数据大约需要8个多小时。我的服务器有6个硬件核心,如何在6个线程中读取这个文件?
此处的LineReader:https://github.com/andrewwoz/LineReader
PS。从一开始的文件每个文件单独1GB。
答案 0 :(得分:0)
从未想过多线程读取带有200gb的.txt文件,但我可能会让控制台检测到有多少核心(e.x.6core)可用并将其拆分为(e.x.6parts)。 ( - >每个流程的一部分) 据我所知,Ubuntu会自动均匀地分发到进程。 希望这有帮助
答案 1 :(得分:0)
!!!此解决方案仅在您使用 POSIX fopen()读取文件时才有效:https://github.com/andrewwoz/LineReader
let reader = LineReader(path: pathToFile)
var threads = [Thread]()
func readTxtFile() {
while let line = reader?.nextLine {
autoreleasepool {
// To do with each line
}}
}
for threadNumber in 0...threadsCount-1 {
threads.append(Thread(){ readTxtFile() })
threads[threadNumber].start()
}
select(0, nil, nil, nil, nil)
实时利润仅限于硬件核心,而不是HT线程。如果您的CPU有2个内核和4个线程,请在代码中使用2个线程。