Ruby processes: Difference between revisions

From wikinotes
No edit summary
 
(5 intermediate revisions by the same user not shown)
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).


<source lang="ruby">
== 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
</source>
</syntaxhighlight>
</blockquote><!-- Backticks -->


<source lang="ruby">
== 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
</source>
</syntaxhighlight>
 
Manipulating STDIN, STDOUT, STDERR.
<syntaxhighlight lang="ruby">
# STDIN from file
Process.spawn("/usr/bin/cat", in: File.open("/home/you/.zshrc", "r"))
 
# STDIN from string
r_pipe, w_pipe = IO.pipe
w_pipe.write("a\nb\nc")
w_pipe.close
Process.spawn("/usr/bin/cat", in: r_pipe)
r_pipe.close
</syntaxhighlight>
 
You can also wrap everything
<syntaxhighlight lang="ruby">
in_r, in_w = IO.pipe
out_r, out_w = IO.pipe
err_r, err_w = IO.pipe
pid = Process.spawn("netstat", "-an", in: in_r, out: out_w, err: err_w)
communicator = OpenStruct.new(pid: pid, in: in_w, out: out_r, err: err_r)
</syntaxhighlight>
</blockquote><!-- Spawn -->
</blockquote><!-- Subprocesses -->


= Fork =
= Fork =
<blockquote>
Fork allows you to run ruby code in a separate process.<br>
Fork allows you to run ruby code in a separate process.<br>


<source lang="ruby">
<syntaxhighlight lang="ruby">
pid = Process.fork do
pid = Process.fork do
   puts "child, pid #{Process.pid} sleeping..."
   puts "child, pid #{Process.pid} sleeping..."
Line 33: Line 64:


Process.wait pid
Process.wait pid
</source>
</syntaxhighlight>


https://naturaily.com/blog/multiprocessing-in-ruby
https://naturaily.com/blog/multiprocessing-in-ruby
</blockquote><!-- Fork -->

Latest revision as of 01:39, 29 July 2023

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 $?.success

Spawn

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

Manipulating STDIN, STDOUT, STDERR.

# STDIN from file
Process.spawn("/usr/bin/cat", in: File.open("/home/you/.zshrc", "r"))

# STDIN from string
r_pipe, w_pipe = IO.pipe
w_pipe.write("a\nb\nc")
w_pipe.close
Process.spawn("/usr/bin/cat", in: r_pipe)
r_pipe.close

You can also wrap everything

in_r, in_w = IO.pipe
out_r, out_w = IO.pipe
err_r, err_w = IO.pipe
pid = Process.spawn("netstat", "-an", in: in_r, out: out_w, err: err_w)
communicator = OpenStruct.new(pid: pid, in: in_w, out: out_r, err: err_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

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