Ruby processes: Difference between revisions
From wikinotes
m (Will moved page Ruby multiprocessing to Ruby processes) |
|||
Line 9: | Line 9: | ||
= Subprocesses = | = Subprocesses = | ||
<blockquote> | |||
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). | ||
< | == Backticks == | ||
<blockquote> | |||
<syntaxhighlight lang="ruby"> | |||
`ls -la | grep foo` | `ls -la | grep foo` | ||
puts "exit code is 0" if $?.success | puts "exit code is 0" if $?.success | ||
</ | </syntaxhighlight> | ||
</blockquote><!-- Backticks --> | |||
< | == Spawn == | ||
<blockquote> | |||
<syntaxhighlight lang="ruby"> | |||
pid = Process.spawn(['ls', '-la']) | pid = Process.spawn(['ls', '-la']) | ||
puts pid # 123456 | puts pid # 123456 | ||
Process.wait pid | Process.wait pid | ||
</ | </syntaxhighlight> | ||
Manipulating STDIN, STDOUT, STDERR. | |||
<syntaxhighlight lang="ruby"> | |||
Process.spawn("cat", in: File.open("/home/you/.zshrc", "r")) | |||
</syntaxhighlight> | |||
</blockquote><!-- Spawn --> | |||
</blockquote><!-- Subprocesses --> | |||
= Fork = | = Fork = |
Revision as of 17:12, 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).
Backticks
`ls -la | grep foo` puts "exit code is 0" if $?.successSpawn
pid = Process.spawn(['ls', '-la']) puts pid # 123456 Process.wait pidManipulating STDIN, STDOUT, STDERR.
Process.spawn("cat", in: File.open("/home/you/.zshrc", "r"))
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