我可以在后台写一个文件吗?

Roj*_*ojj 1 ruby file-io background-process

在执行我的脚本期间,我需要将一个大文件写入磁盘以存储部分计算.这个文件很大,其余的脚本不需要它.目前,我需要等待写入完成才能继续执行脚本.

有没有办法在后台编写文件?有点像启动一个单独的写入过程,而其余的脚本继续不受干扰?

Dan*_*iel 5

thread = Thread.new do
  File.open("output.txt", "w") do |file|
    file.puts "hello from the background"
  end
end

# Finish the script
...

# Wait for the file writing to finish
thread.join
Run Code Online (Sandbox Code Playgroud)