Git Product home page Git Product logo

Comments (6)

mperham avatar mperham commented on July 18, 2024

This is the best I could come up with so far. It hangs waiting for listing.

require 'childprocess'

keywords = %w(redis memcached)

r, w = IO.pipe

# Building "ps aux | grep 'redis|memcached'"
listing = ChildProcess.build("ps", "aux")
listing.io._stdin = File.open('/dev/null')
listing.io.stdout = w
listing.start

search = ChildProcess.build("grep", '-E', keywords.join('|'))
search.io._stdin = r
search.io.stdout = STDOUT
search.start

w.close
r.close
listing.wait
search.wait

puts 'DONE'

from childprocess.

jarib avatar jarib commented on July 18, 2024

The trick here is .duplex = true, which makes sure stdin is set in #start. Then you just need to get the order right.

This should work on at least Unix + JRuby (I don't have access to a Windows machine to test on at the moment):

require 'childprocess'

search = ChildProcess.build("grep", '-E', %w(redis memcached).join('|'))
search.duplex = true
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

puts 'DONE'

Does that work for you?

PS. ❤️ your work!

from childprocess.

mperham avatar mperham commented on July 18, 2024

Thanks, that did the trick. Btw I'm really impressed with the code, but the documentation is weak. The readme needs more content to cover these various features. I'm going to cover childprocess in an upcoming blog post and that's my conclusion.

On 23 Nov 2012, at 17:53, Jari Bakken [email protected] wrote:

The trick here is .duplex = true, which makes sure stdin is set in #start. Then you just need to get the order right.

This should work on at least Unix + JRuby (I don't have access to a Windows machine to test on at the moment):

require 'childprocess'

listing = ChildProcess.build("ps", "aux")

search = ChildProcess.build("grep", '-E', %w(redis memcached).join('|'))
search.duplex = true
search.io.stdout = STDOUT
search.start

listing.io.stdout = search.io.stdin
listing.start
listing.wait

search.io.stdin.close
search.wait

puts 'DONE'
Does that work for you?

PS. your work!


Reply to this email directly or view it on GitHub.

from childprocess.

jarib avatar jarib commented on July 18, 2024

Yeah, the README definitely needs improvement.

from childprocess.

jarib avatar jarib commented on July 18, 2024

I added some more examples to the README. Let me know if there's anything specific you'd like to see there.

from childprocess.

mperham avatar mperham commented on July 18, 2024

Fantastic, much better now. Thanks so much!

from childprocess.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.