|
1 | 1 | module POSIX
|
2 | 2 | module Spawn
|
3 | 3 | # POSIX::Spawn::Child includes logic for executing child processes and
|
4 |
| - # reading/writing from their standard input, output, and error streams. |
| 4 | + # reading/writing from their standard input, output, and error streams. It's |
| 5 | + # designed to take all input in a single string and provides all output |
| 6 | + # (stderr and stdout) as single strings and is therefore not well-suited |
| 7 | + # to streaming large quantities of data in and out of commands. |
5 | 8 | #
|
6 |
| - # Create an run a process to completion: |
| 9 | + # Create and run a process to completion: |
7 | 10 | #
|
8 |
| - # >> process = POSIX::Spawn::Child.new(['git', '--help']) |
| 11 | + # >> child = POSIX::Spawn::Child.new('git', '--help') |
9 | 12 | #
|
10 | 13 | # Retrieve stdout or stderr output:
|
11 | 14 | #
|
12 |
| - # >> process.out |
| 15 | + # >> child.out |
13 | 16 | # => "usage: git [--version] [--exec-path[=GIT_EXEC_PATH]]\n ..."
|
14 |
| - # >> process.err |
| 17 | + # >> child.err |
15 | 18 | # => ""
|
16 | 19 | #
|
17 | 20 | # Check process exit status information:
|
18 | 21 | #
|
19 |
| - # >> process.status |
| 22 | + # >> child.status |
20 | 23 | # => #<Process::Status: pid=80718,exited(0)>
|
21 | 24 | #
|
22 |
| - # POSIX::Spawn::Child is designed to take all input in a single string and |
23 |
| - # provides all output as single strings. It is therefore not well suited |
24 |
| - # to streaming large quantities of data in and out of commands. |
| 25 | + # To write data on the new process's stdin immediately after spawning: |
| 26 | + # |
| 27 | + # >> child = POSIX::Spawn::Child.new('bc', :input => '40 + 2') |
| 28 | + # >> child.out |
| 29 | + # "42\n" |
25 | 30 | #
|
26 |
| - # Q: Why not use popen3 or hand-roll fork/exec code? |
| 31 | + # Q: Why use POSIX::Spawn::Child instead of popen3, hand rolled fork/exec |
| 32 | + # code, or Process::spawn? |
27 | 33 | #
|
28 | 34 | # - It's more efficient than popen3 and provides meaningful process
|
29 | 35 | # hierarchies because it performs a single fork/exec. (popen3 double forks
|
30 | 36 | # to avoid needing to collect the exit status and also calls
|
31 | 37 | # Process::detach which creates a Ruby Thread!!!!).
|
32 | 38 | #
|
33 |
| - # - It's more portable than hand rolled pipe, fork, exec code because |
34 |
| - # fork(2) and exec(2) aren't available on all platforms. In those cases, |
35 |
| - # POSIX::Spawn::Child falls back to using whatever janky substitutes the platform |
36 |
| - # provides. |
| 39 | + # - It handles all max pipe buffer (PIPE_BUF) hang cases when reading and |
| 40 | + # writing semi-large amounts of data. This is non-trivial to implement |
| 41 | + # correctly and must be accounted for with popen3, spawn, or hand rolled |
| 42 | + # fork/exec code. |
37 | 43 | #
|
38 |
| - # - It handles all max pipe buffer hang cases, which is non trivial to |
39 |
| - # implement correctly and must be accounted for with either popen3 or |
40 |
| - # hand rolled fork/exec code. |
| 44 | + # - It's more portable than hand rolled pipe, fork, exec code because |
| 45 | + # fork(2) and exec aren't available on all platforms. In those cases, |
| 46 | + # POSIX::Spawn::Child falls back to using whatever janky substitutes |
| 47 | + # the platform provides. |
41 | 48 | class Child
|
42 | 49 | include POSIX::Spawn
|
43 | 50 |
|
44 |
| - # Create and execute a new process. |
| 51 | + # Spawn a new process, write all input and read all output, and wait for |
| 52 | + # the program to exit. Supports the standard spawn interface as described |
| 53 | + # in the POSIX::Spawn module documentation: |
45 | 54 | #
|
46 |
| - # argv - Array of [command, arg1, ...] strings to use as the new |
47 |
| - # process's argv. When argv is a String, the shell is used |
48 |
| - # to interpret the command. |
49 |
| - # env - The new process's environment variables. This is merged with |
50 |
| - # the current environment as if by ENV.merge(env). |
51 |
| - # options - Additional options: |
52 |
| - # :input => str to write str to the process's stdin. |
53 |
| - # :timeout => int number of seconds before we given up. |
54 |
| - # :max => total number of output bytes |
55 |
| - # A subset of Process::spawn options are also supported on all |
56 |
| - # platforms: |
57 |
| - # :chdir => str to start the process in different working dir. |
| 55 | + # new([env], command, [argv1, ...], [options]) |
58 | 56 | #
|
59 |
| - # Returns a new Child instance that has already executed to completion. |
60 |
| - # The out, err, and status attributes are immediately available. |
61 |
| - def initialize(*argv) |
62 |
| - env, argv, options = extract_process_spawn_arguments(*argv) |
63 |
| - @argv = argv |
64 |
| - @env = env |
65 |
| - |
| 57 | + # The following options are supported in addition to the standard |
| 58 | + # POSIX::Spawn options: |
| 59 | + # |
| 60 | + # :input => str Write str to the new process's standard input. |
| 61 | + # :timeout => int Maximum number of seconds to allow the process |
| 62 | + # to execute before aborting with a TimeoutExceeded |
| 63 | + # exception. |
| 64 | + # :max => total Maximum number of bytes of output to allow the |
| 65 | + # process to generate before aborting with a |
| 66 | + # MaximumOutputExceeded exception. |
| 67 | + # |
| 68 | + # Returns a new Child instance whose underlying process has already |
| 69 | + # executed to completion. The out, err, and status attributes are |
| 70 | + # immediately available. |
| 71 | + def initialize(*args) |
| 72 | + @env, @argv, options = extract_process_spawn_arguments(*args) |
66 | 73 | @options = options.dup
|
67 | 74 | @input = @options.delete(:input)
|
68 | 75 | @timeout = @options.delete(:timeout)
|
69 | 76 | @max = @options.delete(:max)
|
70 | 77 | @options.delete(:chdir) if @options[:chdir].nil?
|
71 |
| - |
72 | 78 | exec!
|
73 | 79 | end
|
74 | 80 |
|
|
0 commit comments