0

Thread.current这段代码有什么用?我正在查看在Rails 应用程序中使用 DCI 的示例在 lib/context.rb 中,有这样的:

module Context
  include ContextAccessor

  def context=(ctx)
    Thread.current[:context] = ctx
  end

  def in_context
    old_context = self.context
    self.context = self
    res = yield
    self.context = old_context
    res
  end
end

它用于 app/contexts 中的各种上下文,例如:

def bid
  in_context do
    validator.validate
    biddable.create_bid
  end
  #...
end

in_context在块中运行代码并在当前线程上设置键值对有什么好处?

4

2 回答 2

3

通常,在块内部,您无法访问调用者的上下文(除了闭包变量。)

▶ class A
▷   attr_accessor :a
▷   def test
▷     yield if block_given?
▷   end  
▷ end  
#⇒ :test
▶ inst = A.new
#⇒ #<A:0x00000006f41e28>
▶ inst.a = 'Test'
#⇒ "Test"
▶ inst.test do 
▷   puts self
▷   # here you do not have an access to inst at all
▷   # this would raise an exception: puts self.a
▷ end
#⇒ main

但是有了上下文,您仍然可以inst从内部块访问:

▶ in_context do 
▷   puts self.context.a
▷ end
#⇒ 'Test'

因此,人们可能会介绍proc(考虑两者AB包括Context在内):

▶ pr = ->() { puts self.context }
▶ A.new.in_context &pr
#⇒ #<A:0x00000006f41e28>
▶ B.new.in_context &pr
#⇒ #<B:0x00000006f41eff>

现在 externalproc pr几乎可以完全访问它的调用者

于 2015-09-02T08:21:23.580 回答
2

Thread.current需要支持多线程应用程序,其中每个线程都有自己的上下文。

还包括一个ContextAccessor模块,可以在其中获取上下文。将它们放在一起只会给出更清晰的画面:

# context.rb
def context=(ctx)
  Thread.current[:context] = ctx
end

# context_accessor.rb
def context
  Thread.current[:context]
end

in_context方法旨在安全地更改其块内的上下文。无论更改是什么,当块结束执行时,旧的上下文都会恢复。

于 2015-09-02T08:24:17.193 回答