如何调用多个过程?

发布于 2024-11-29 08:05:51 字数 410 浏览 1 评论 0原文

给定以下代码,我可以在这方面使用一些帮助:

result1, result2, result3 = do_stuff {
  method_1
  method_2
  method_3
}

我希望能够编写一个名为 do_stuff 的方法,该方法可以单独调用该块的每一行并返回每行/块的结果。能做到吗?我是否以错误的方式处理这个问题?我正在想这样的事情(根本不起作用)。

def do_stuff(&block)
  block.each_block do |block|
    block.call
  end
end

编辑:我想要完成的是能够并行运行方法“do_stuff”内的每个方法/块调用(在它自己的线程中),并在每个方法调用周围添加一些日志记录。

I could use some help on this one, given this code:

result1, result2, result3 = do_stuff {
  method_1
  method_2
  method_3
}

I would like to be able to write a method called do_stuff that can call each line of that block individually and return a result for each line/block. Can it be done? Am I going about this the wrong way? Something like this (doesn't work at all) is what I am thinking.

def do_stuff(&block)
  block.each_block do |block|
    block.call
  end
end

EDIT: What I am trying to accomplish is to be able to run each method/block call inside the method "do_stuff" in parallel (in it's own thread) and also add some logging around each method call.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梅窗月明清似水 2024-12-06 08:05:51

我同意上面的 mu ,你应该解释你想要做什么,因为可能有更合适的模式可以使用。

顺便说一句,你可以做你所要求的一个小的改变:

result1, result2 = do_stuff {
  [
    method_1,
    method_2,
    method_3
  ]
}

或者,也许,更优雅,没有块:

result1, result2 = [
  method_1,
  method_2,
  method_3
]

:)

I agree with mu above, you should explain what you are trying to do, as there is probably a more suitable pattern to use.

BTW, you can do what you ask for with a minor change:

result1, result2 = do_stuff {
  [
    method_1,
    method_2,
    method_3
  ]
}

or, perhaps, more elegantly, without the block:

result1, result2 = [
  method_1,
  method_2,
  method_3
]

:)

贪恋 2024-12-06 08:05:51

好的,问题更新后看起来更清楚了。您可以使用 method_missinginstance_eval 和线程执行类似的操作:

class Parallelizer
  class << self
    def run(receiver, &block)
      @receiver = receiver
      instance_eval &block
      # wait for all threads to finish
      @threads.each{|t| t.join}
      @results
    end

    def method_missing *args, &block
      @threads ||= []
      @results ||= []
      @threads.push Thread.new{
        # you could add here custom wrappings
        @results.push(@receiver.send(*args, &block))
      }
    end
  end
end

class Test
  def take_a_break name, sec
    puts "#{name} taking a break for #{sec} seconds"
    Kernel.sleep sec
    puts "#{name} done."
    name
  end
end

t = Test.new

results = Parallelizer.run(t) do
  take_a_break 'foo', 3
  take_a_break 'bar', 2
  take_a_break 'baz', 1
end

不过请注意,这还没有经过充分测试,而且我不确定线程​​安全性如何。

OK, it looks clearer after the question was updated. You could do something like this, using method_missing, instance_eval and threads:

class Parallelizer
  class << self
    def run(receiver, &block)
      @receiver = receiver
      instance_eval &block
      # wait for all threads to finish
      @threads.each{|t| t.join}
      @results
    end

    def method_missing *args, &block
      @threads ||= []
      @results ||= []
      @threads.push Thread.new{
        # you could add here custom wrappings
        @results.push(@receiver.send(*args, &block))
      }
    end
  end
end

class Test
  def take_a_break name, sec
    puts "#{name} taking a break for #{sec} seconds"
    Kernel.sleep sec
    puts "#{name} done."
    name
  end
end

t = Test.new

results = Parallelizer.run(t) do
  take_a_break 'foo', 3
  take_a_break 'bar', 2
  take_a_break 'baz', 1
end

Be careful, though, that this is not well-tested and I am not sure how threadsafe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文