Skip to content

Commit

Permalink
Add more examples to the README. Closes #43.
Browse files Browse the repository at this point in the history
  • Loading branch information
jarib committed Nov 24, 2012
1 parent a650ecd commit 62c93a8
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ coverage
rdoc
pkg
.rbx
Gemfile.lock

## PROJECT::SPECIFIC
78 changes: 71 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
childprocess
============
# childprocess

This gem aims at being a simple and reliable solution for controlling
external programs running in the background on any Ruby / OS combination.
Expand All @@ -9,8 +8,12 @@ a standalone library.

[![Build Status](https://secure.travis-ci.org/jarib/childprocess.png)](http://travis-ci.org/jarib/childprocess)

Usage
-----
# Usage

The object returned from ChildProcess.build will implement ChildProcess::AbstractProcess.

### Basic examples

```ruby
process = ChildProcess.build("ruby", "-e", "sleep")

Expand All @@ -24,6 +27,9 @@ process.io.stdout = Tempfile.new("child-output")
process.environment["a"] = "b"
process.environment["c"] = nil

# set the child's working directory
process.cwd = '/some/path'

# start the process
process.start

Expand All @@ -35,6 +41,10 @@ process.exited? #=> false
process.wait
process.exited? #=> true

# get the exit code

process.exit_code #=> 0

# ...or poll for exit + force quit
begin
process.poll_for_exit(10)
Expand All @@ -43,10 +53,64 @@ rescue ChildProcess::TimeoutError
end
```

The object returned from ChildProcess.build will implement ChildProcess::AbstractProcess.
### Advanced examples

#### Write to stdin

```ruby
process = ChildProcess.build("cat")

out = Tempfile.new("duplex")
out.sync = true

process.io.stdout = process.io.stderr = out
process.duplex = true

process.start
process.io.stdin.puts "hello world"
process.io.stdin.close

process.poll_for_exit(exit_timeout_in_seconds)

out.rewind
out.read #=> "hello world\n"
```

#### Pipe output to another ChildProcess

```ruby
search = ChildProcess.build("grep", '-E', %w(redis memcached).join('|'))
search.duplex = true # sets up pipe so process.io.stdin will be available after .start
search.io.stdout = $stdout
search.start

listing = ChildProcess.build("ps", "aux")
listing.io.stdout = search.io.stdin
listing.start
listing.wait

search.io.stdin.close
search.wait
```

#### Prefer posix_spawn on *nix

If the parent process is using a lot of memory, `fork+exec` can be very expensive. The `posix_spawn()` API removes this overhead.

```
ChildProcess.posix_spawn = true
process = ChildProcess.build(*args)
```

#### Detach from parent

```
process = ChildProcess.build("sleep", "10")
process.detach = true
process.start
```

Implementation
--------------
# Implementation

How the process is launched and killed depends on the platform:

Expand Down

0 comments on commit 62c93a8

Please sign in to comment.