In my MRI-targeted code I use the forkoff¹ gem, which adds the
Enumerable#forkoff method that’s basically a #map running its &block’s
contents in separate processes.
¹ http://codeforpeople.com/lib/ruby/forkoff/forkoff-0.0.4/README
In my code, I incorporated #forkoff like this:
module Enumerable
def parallel &block
case ArtDecomp::Conf.processes
when 1 then map(&block)
else forkoff ArtDecomp::Conf.processes, &block
end
end
end
Trying to run #parallel with Conf.processes set to 2 in JRuby gives me
the (understandable) ‘fork is unsafe and disabled by default on JRuby’
error.
What would be the JRuby’s equivalent? In other words:
how would one run such code in parallel properly in JRuby?
Confession 1: I know I should figure the above by myself, but I haven’t
yet dug into JRuby, I’m just evaluating its purpose for my case (PhD
computations) – I need to run my code faster, and I want to see how
fast I can go with JRuby before I give up on pure-Ruby speed and go
the one-way road of RubyInline…
Confession 2: My knowledge about threads and processes is murky to say
the least. All I know is that MRI’s global interpreter lock means two
‘parallel’ threads aren’t really parallel and won’t use multiple cores
while two processes will; also, IIRC, JRuby maps threads to JVM’s
processes and so JRuby threads are really parallel – if that’s true,
I’m ok with running my code in threads under JRuby.
Confession 3: I should be using DRb for the above anyway, and that’s the
plan for the future, but for now I need code that utilises two cores in
full.
– Shot