What’s the difference between Process.fork and the new Process.spawn methods in Ruby 1.9.2 and which one is better to run another program in a subprocess? As far as I understand Process.fork accepts block of code and Process.spawn takes a system command plus some other parameters. When I should use one instead of the other?
What’s the difference between Process.fork and the new Process.spawn methods in Ruby 1.9.2 and
Share
Process.forkallows you to run ruby code in another process.Process.spawnallows you to run another program in another process. BasicallyProcess.spawnis like usingProcess.forkand then callingexecin the forked process, except that it gives you more options.If you need backwards compatibility, use
fork+execasspawnis not available in 1.8. Otherwise usespawnsince running another program in a subprocess is exactly whatspawnis made for.Exactly.
Use
forkif you need to run arbitrary ruby code in a separate process (you can’t do that withspawn). Usespawnif you need to invoke an application in a subprocess.