Process, Thread, Whatever

Posted by Nick Sieger Fri, 16 Mar 2007 04:18:00 GMT

While cleaning up the subprocess launching code in JRuby recently, I pulled a fast one. See, JRuby has this little trick we call an in-process script that allows launching of a separate JRuby runtime in the same process. Basically, we look for the command-line passed to Kernel#system or Kernel#backtick, and if the program name contains “ruby” or ends in “.rb”, we assume the caller wants to launch another JRuby interpreter and launch the command in the same process instead of spawning a new one. Set aside for a moment the question of whether this is “the right thing” to do.

In my desire to dry up this code a bit, but also to extend the same behavior for IO.popen, I needed the in-process script to behave like a real process. So hey, why not extend java.lang.Process? The details are in the ShellLauncher code, but it turns out, oddly enough, this actually works.

I think this points out an interesting feature of JRuby -- that it’s process-agnostic. You can map JRuby runtimes M:N with processes in any combination you like. In fact, this is what’s currently being done with the Rails Integration (Rails-in-a-war-file) code -- a pool of JRuby runtimes preloaded with Rails are used to run the Rails application from a Java servlet. Robert Egglestone’s been doing some great work with Rails Integration and the possibilities are only starting to reveal themselves.

Tags  | no comments | no trackbacks



Use the following link to trackback from your own site: