假设我在 Julia v1.1 中有一个非常大的文本文件(30GB),我想要使用并行处理(多线程)来读取每一行,应该怎么做?
在查看Julia的多线程文档后,这段代码是尝试实现以上功能的,但是它根本不起作用。
open("pathtofile", "r") do file
# Count number of lines in file
seekend(file)
fileSize = position(file)
seekstart(file)
# skip nseekchars first characters of file
seek(file, nseekchars)
# progress bar, because it's a HUGE file
p = Progress(fileSize, 1, "Reading file...", 40)
Threads.@threads for ln in eachline(file)
# do something on ln
u, v = map(x->parse(UInt32, x), split(ln))
.... # other interesting things
update!(p, position(file))
end
end
注意1:您需要使用ProgressMeter
(我希望我的代码在并行读取文件时显示进度条)
注意2:nseekchars是一个整数,表示我想要在文件开头跳过的字符数
注意3:该代码可以工作,但是如果没有在for循环旁边使用Threads.@threads
宏,则不会进行并行处理