Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I launch multiple rakes in parallel from a ruby script

Tags:

ruby

rake

I have a ruby script from which I want to launch 4 rake tasks to run in parallel.

How do I do this? I think I'll need to Fork and detach a process but I need the exact syntax.

like image 436
Nick Avatar asked Apr 09 '11 16:04

Nick


Video Answer


2 Answers

It's better if you let Rake handle the parallelism. You can do that using "multitask." Inside the Rakefile:

desc "Start everything."
multitask :start => [ 'mongodb:start', 'haystack:start' ]

Background and source.

Otherwise, presuming you are doing this from outside the Rakefile, you could use horrid code like this, which would not throw exceptions as you might expect, and could easily fail in a number of ways:

require 'rake'
load 'Rakefile'

def invoke(name)
  Thread.new do
    puts Rake::application[name].invoke
  end
end

invoke :make_coffee
invoke :boil_eggs
invoke :empty_trash

(so don't do that)

like image 135
Jostein Avatar answered Nov 11 '22 16:11

Jostein


use https://github.com/grosser/parallel

Parallel.each(data, :in_processes => 4) { |x| ruby_function(x) }

like image 36
Mathieu J. Avatar answered Nov 11 '22 17:11

Mathieu J.