Issue #4649 has been updated by Joel VanderWerf.


IMO this is not really about parallel computation, but about syntax and scope, and it can be solved without changing ruby.

One solution that seems fairly idiomatic to me is:

def get_response_from_service_a; "a response"; end
def get_response_from_service_b; "b response"; end

class Parallels
  def initialize
    @tasks = []
  end
  def task &task
    @tasks << Thread.new(&task)
  end
  def values
    @tasks.map {|task| task.value}
  end
end

def parallels &task_block
  par = Parallels.new
  par.instance_eval &task_block
  par.values
end

a, b = parallels do
  task { get_response_from_service_a }
  task { get_response_from_service_b }
end

p a, b

----------------------------------------
RubySpec #4649: Adding parallel constructors to Ruby 2.0
http://redmine.ruby-lang.org/issues/4649

Author: Rodrigo Rosenfeld Rosas
Status: Open
Priority: Normal
Assignee: Yukihiro Matsumoto
Category: 
Target version: 2.0


I was not sure if this was RubySpec or Feature type.

My request is to create some new syntax for easing the write of concurrent code with Ruby. The syntax could be something like this:

parallels do
  task "get response from service A" do # optional description
    a = IO.read("http://serviceA/request")
  end
  task { b = get_response_from_service_b }
end

# at this point both tasks have finish, it's like a join in the threads at the end of the parallel block.
call_another_service_that_depends_on(a and b)

I am not sure, though, how to deal with scopes inside the parallel block. Traditionally in Ruby, this wouldn't work because both 'a' and 'b' would be local to the 'task' block and would not be accessible from the outside of the parallel block. If we don't want instance variables, I don't know what is the best approach for avoiding some "a=nil; b=nil" before the parallel block.

Maybe this could start as a gem, but having it implemented directly in Ruby would be awesome, specially for scripts that usually don't rely in external dependencies for simplifying distribution...

Any thoughts about this propose?


-- 
http://redmine.ruby-lang.org