Nick Sieger: Process, Thread, Whatever http://blog.nicksieger.com/articles/2007/03/16/process-thread-whatever en-us 40 do what you love Process, Thread, Whatever <p>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 <code>Kernel#system</code> or <code>Kernel#backtick</code>, and if the program name contains &#8220;ruby&#8221; or ends in &#8220;.rb&#8221;, 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 &#8220;the right thing&#8221; to do.</p> <p>In my desire to dry up this code a bit, but also to extend the same behavior for <code>IO.popen</code>, I needed the in-process script to behave like a real process. So hey, why not extend <code>java.lang.Process</code>? The details are in the <a href="http://svn.jruby.codehaus.org/browse/jruby/trunk/jruby/src/org/jruby/util/ShellLauncher.java">ShellLauncher</a> code, but it turns out, oddly enough, this actually works.</p> <p>I think this points out an interesting feature of JRuby &#8211; that it&#8217;s process-agnostic. You can map JRuby runtimes M:N with processes in any combination you like. In fact, this is what&#8217;s currently being done with the <a href="http://www.headius.com/jrubywiki/index.php/Rails_Integration">Rails Integration</a> (Rails-in-a-war-file) code &#8211; a pool of JRuby runtimes preloaded with Rails are used to run the Rails application from a Java servlet. Robert Egglestone&#8217;s been doing some great work with Rails Integration and the possibilities are only starting to reveal themselves.</p> Fri, 16 Mar 2007 04:18:00 +0000 urn:uuid:aedaaefd-543f-496b-ad12-5d0027b233ce Nick Sieger http://blog.nicksieger.com/articles/2007/03/16/process-thread-whatever jruby http://blog.nicksieger.com/articles/trackback/224