1

During the execution of my script I need to write a big file to disk to store partial calculations. This file is quite big and it is not needed for the rest of the script. At the moment I need to wait for the writing to finish before I can continue the execution of the script.

Is there a way to write the file in background? Something like launching a separate process for writing while the rest of the script continues undisturbed?

sawa
  • 165,429
  • 45
  • 277
  • 381
Rojj
  • 1,170
  • 1
  • 12
  • 32
  • Wow! That was fast. Thanks, it works. I assume the `thread.join` is needed to wait for the writing to finish before the script ends. – Rojj Sep 05 '14 at 09:46

1 Answers1

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
Daniel
  • 10,115
  • 3
  • 44
  • 62