我正在编写的方法的目标是并行运行块参数中调用的每个方法。
我正在探索使用TracePoint,我正在使用它来确定何时调用块中的每个方法,在那里我派生一个进程并调用该方法,但是我找不到一种方法来停止默认方法的执行。
我尝试在TracePoint块中使用define_singleton_method重新定义被调用的方法,这会为下一次执行正确地更改该方法,但当前的执行仍然原封不动。在TracePoint中有没有一种方法可以停止当前被跟踪的方法的执行?或者,有没有一种不使用TracePoint就能实现目标的方法?
下面是一些简化的代码来说明:
def my_method(&block)
trace = TracePoint.new(:call) do |tp|
tp.disable
method = tp.self.method(tp.method_id)
@args = []
method.parameters.each { |param|
@args.push(tp.binding.eval("#{param.last}"))
}
fork do
# This executes the method in it's own process with the same
# arguments given in the traced call
result = method.call(*@args)
end
tp.enable
end
trace.enable
block.call
trace.disable
end在我的代码在TracePoint块中执行之后,在block.call返回之前,块中的方法在主进程中再次执行。我怎样才能防止这种情况发生呢?
发布于 2016-06-12 23:02:56
也许你可以在你的TracePoint回调中引发一个异常,然后在外部捕获并忽略它。
https://stackoverflow.com/questions/36924623
复制相似问题