1

我正在尝试在 ruby​​ 中评估 javascript。

config/initializer/initializer_context.rb
  js_str = 
  EXEC_PP_CONTEXT = ExecJS.compile("function test_add(param) { return param.a+ param.b;}")

然后在我的控制器中我正在使用:

data_hash = {:a=>4,:b=>5}
EXEC_PP_CONTEXT.exec("return test_add(#{data_hash.to_json})")

但我偶尔会收到此错误(100 个请求中有 1 个)

can not use Context instance already associated with some thread

堆:

  • ruby 1.9 模式下的 jruby-1.7.3
  • jruby_min_runtimes & jruby_min_runtimes 为 1 的特立尼达服务器
  • 导轨 3.2.13
  • therubyrhino 2.0.2

如果我已经将最大运行时间和最小运行时间设置为 1,它不应该首先避免这个问题吗?

4

1 回答 1

0

finally figured out by reading Mozilla rhino's code: https://github.com/matthieu/rhymeno/blob/master/src/org/mozilla/javascript/Context.java#LC416

static final Context enter(Context cx, ContextFactory factory) Basically the method counts the number of threads that are inside the context ( using it / executing it) and if another thread tries to enter it , it throws an error.

To avoid the problem and achieve concurrency , we lazily created one context per thread using ruby's Thread[:current][:some_js_context] = blah_

于 2015-02-24T07:31:38.503 回答