Howto multithreaded jython scripts running from java?

I'm constructing a framework in Java that will listen for events and then process them in Jython. Different event types will be sent to different scripts. Since jython takes quite some time to compile the script when PythonInterpreter.exec() is called, I will have to pre-compile the scripts. I'm doing it the following way: // initialize the script as string (would load it from file in final version) String script = "print 'foo'"; // get the compiled code object PyCode compiled = org.python.core.__builtin__.compile( script, "<>", "exec" ); The PyCode compiled object would be pushed to repository and used as events come in PythonInterpreter pi = new PythonInterpreter(); pi.set( "variable_1", "value_1"); pi.set( "variable_x", "value_x"); pi.exec( compiled ); Now for my conundrum - it might happen that there are multiple events of certain type happening at the same time - thus multiple instances of script running at the same time. Almost all scripts would probably remain short-lived - up to 100 lines, no loops. Number and frequency is completely random (user generated events) and could be from 0 to about 200 per second per event type. What would the best way to do it be? I'm looking at a few possibilities: 1. use synchronization at trigger event point - this would prevent multiple instances of same script but also events wouldn't be processed as quickly as they should be 2. create a pool of same type scripts somehow populated by cloning original PyCode object - the biggest problem would probably be optimizing pool sizes 3. dynamically clone the script object from the parent whenever needed and then discard it when exec() finishes - this way the lag is removed from compile but it is still present in clone method Probably the combination of number 2 and 3 would be the best - creating dynamic pool sizes? So, any thoughts? ;)

以上就是Howto multithreaded jython scripts running from java?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准