简体   繁体   English

Jruby线程上下文实例关联

[英]Jruby thread context instance association

I am trying to eval javascript in ruby. 我正在尝试在ruby中评估javascript。

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

and then in my controller I am using : 然后在我的控制器中,我正在使用:

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

But I occasionally get this error ( 1 in 100 requests) 但我偶尔会收到此错误(每100个请求中有1个)

can not use Context instance already associated with some thread

Stack: 堆:

  • jruby-1.7.3 in ruby 1.9 mode 红宝石1.9模式下的jruby-1.7.3
  • trinidad server with jruby_min_runtimes & jruby_min_runtimes as 1 特立尼达服务器,jruby_min_runtimes和jruby_min_runtimes为1
  • rails 3.2.13 导轨3.2.13
  • therubyrhino 2.0.2 Therubyrhino 2.0.2

If I am already setting max runtime and min runtime to 1 shouldn't it avoid this problem in first place ? 如果我已经将最大运行时间和最小运行时间设置为1,那么它是否应该首先避免此问题?

finally figured out by reading Mozilla rhino's code: https://github.com/matthieu/rhymeno/blob/master/src/org/mozilla/javascript/Context.java#LC416 最终通过阅读Mozilla Rhino的代码找到了答案: 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. static final Context enter(Context cx, ContextFactory factory)基本上,该方法计算上下文(使用它/执行它)内部的线程数,如果另一个线程尝试输入它,则抛出错误。

To avoid the problem and achieve concurrency , we lazily created one context per thread using ruby's Thread[:current][:some_js_context] = blah_ 为了避免该问题并实现并发,我们使用ruby的Thread[:current][:some_js_context] = blah_懒惰地为每个线程创建一个上下文

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM