As the previous posts of this blog, ruby 1.9's Kernel.spawn is very useful. I ported it to ruby 1.8 partly. How about on JRuby?
Currently there are two ways of using spawn on JRuby, but both of them have problem.
fork + exec + spawn-for-legacy library
I made a wrapper library spawn-for-legacy which provides ruby 1.9 style spawn, using fork and exec.
If JRuby has fork and exec, you can use spawn-for-legacy. Unfortunatelly when you use fork on JRuby, you have to set a command line option to JRuby interpreter.
It also outputs an warning.
I like this philosophy. JRuby only offeres platform independent features as the default. If you want to use platform dependent features, you need to state it explicitly.
Anyway, now you can use spawn on the platform provides fork.
#!/jruby -J-Djruby.fork.enabled=true
require 'rubygems'
require 'sfl'
pid = spawn 'ruby', 'a.rb', :out => '/dev/null'
spoon library
spoon is a rubygems library written by Charles Nutter. This offers spawn using ffi. This works all platforms which provide posix_spawn. Usually the number of platforms which provide posix_spawn is more than the number of platforms which provide fork, so spoon is more platform independent.
#!/jruby
require 'rubygems'
require 'spoon'
pid = spawn 'ruby', 'a.rb'
Unfortunatelly current spoon doesn't seem to support all features ruby 1.9's spawn has. For example, you cannot redirect the output.
Future
The goal is already clear. I think someone will write a wrapper library.

No comments:
Post a Comment