Ruby processes

From wikinotes
Revision as of 19:48, 17 February 2020 by Will (talk | contribs) (→‎Fork)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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