Ruby processes: Difference between revisions

From wikinotes
(No difference)

Revision as of 19:48, 17 February 2020

Subprocesses

Process functions create/operate on a PID (instead of an object representing the process, like python).

pid = Process.spawn(['ls', '-la'])
puts pid  # 123456
Process.wait pid

Fork

Fork allows you to run ruby code in a separate process.

pid = Process.fork do
  puts "child, pid #{Process.pid} sleeping..."
  sleep 5
  puts "child exiting"
end

Process.wait pid

https://naturaily.com/blog/multiprocessing-in-ruby