Ruby processes: Difference between revisions

From wikinotes
No edit summary
Line 1: Line 1:
= Documentation =
<blockquote>
{| class="wikitable"
|-
| Process docs || https://docs.ruby-lang.org/en/2.7.0/Process.html
|-
|}
</blockquote><!-- Documentation -->
= Subprocesses =
= Subprocesses =
Process functions create/operate on a PID (instead of an object representing the process, like python).
Process functions create/operate on a PID (instead of an object representing the process, like python).

Revision as of 16:59, 29 October 2022

Documentation

Process docs https://docs.ruby-lang.org/en/2.7.0/Process.html

Subprocesses

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

`ls -la | grep foo`
puts "exit code is 0" if $?.success
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