Skip to content

Ruby, popen and session

In the past two days I tried to rewrite a bunch of popen based code that drives an external tool I need for a webapp I’m coding (in Rails). The problems arose because IO::popen hung when used behind the webapp with a 5K+ input sent it from the standard input. In my “journey” I learnt about RubyInline, nice tool to wrap C/C++ code, I learnt more about IPC world, Ruby support for I/O and more so I was happy learning new stuff but my problem was still there.

Here comes session library (and his author Ara T. Howard) to the rescue :)

His latest version (2.4.0 at the time) solves all my problems with hangs.

Good job Ara, very good job.

ps. here is the solution

cmd = 'theprocesstodrive'
text = 'thetexttopasstothestdinoftheaboveprocess'
session = Session.new
result = session.execute cmd, :stdin => text
Nice, isn’t it?

Share and Enjoy:
  • Print
  • Digg
  • StumbleUpon
  • Facebook
  • Twitter
  • Google Bookmarks
  • FriendFeed
  • Google Buzz
  • HackerNews
  • Posterous
  • Reddit
  • Slashdot
  • Tumblr
  • Max

    Thanks a lot for this post man. I just upgraded to Hardy Heron and re-setup my environment and my Open3.popen3 code stopped working, and session is just the ticket! It’s too bad that I have to install another gem, but it’s not that big a deal. Thanks again.