inicio mail me! sindicaci;ón

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?

Related posts

  • The Philosophy of Ruby
  • Italian Ruby book and my chapter
  • “Ruby per applicazioni web” is out!
  • Updates from Python SVN, Part 7
  • Django from another perspective
  • Gravatar

    Max said,

    April 27, 2008 @ 1:18 am

    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.

    RSS feed for comments on this post · TrackBack URI

    Leave a Comment