JPPF, java, parallel computing, distributed computing, grid computing, parallel, distributed, cluster, grid, cloud, open source, android, .net
JPPF

The open source
grid computing
solution

 Home   About   Features   Download   Documentation   On Github   Forums 
November 18, 2019, 02:32:19 AM *
Welcome,
Please login or register.

Login with username, password and session length
Advanced search  
News: New users, please read this message. Thank you!
  Home Help Search Login Register  
Pages: [1]   Go Down

Author Topic: execute a jython script  (Read 2867 times)

georgiuk

  • JPPF Padawan
  • *
  • Posts: 2
execute a jython script
« on: July 12, 2011, 12:11:58 AM »

Hello,
is there a way to execute a jython script in parallel with JPPF?
Can the scripting runner api be used for this purpose ans how?
Thanx
Kostas
Logged

lolo

  • Administrator
  • JPPF Council Member
  • *****
  • Posts: 2256
    • JPPF Web site
Re: execute a jython script
« Reply #1 on: July 12, 2011, 10:50:07 AM »

Hello Kostas,

I was able to run a Jython script from a JPPF task, using the PythonInterpreter API.
Here is the example I used:

The JPPF task:

Code: [Select]
public class JythonTask extends JPPFTask {
  private static final long serialVersionUID = 1L;
  private String script = null;

  public JythonTask(String script) {
    this.script = script;
  }

  public void run() {
    try {
      System.out.println("starting task");
      PythonInterpreter interp = new PythonInterpreter();
      interp.exec(script);
      PyObject result = interp.get("result");
      System.out.println("result: " + result);
      setResult(result);
    } catch (Exception e)  {
      e.printStackTrace();
      setException(e);
    }
  }
}

The code that runs the task on the grid:

Code: [Select]
public class JythonRunner {
  private static JPPFClient jppfClient = null;

  public static void main(String...args) {
    try {
      jppfClient = new JPPFClient();
      String script = FileUtils.readTextFile("Task.py");
      JPPFJob job = new JPPFJob();
      job.addTask(new JythonTask(script));
      List<JPPFTask> results = jppfClient.submit(job);
      JPPFTask task = results.get(0);
      if (task.getException() != null) throw task.getException();
      else System.out.println("task result : " + task.getResult());
    } catch(Exception e) {
      e.printStackTrace();
    } finally {
      if (jppfClient != null) jppfClient.close();
    }
  }
}

and finally the Python script executed by the task:

Code: [Select]
"""\
Test script
"""

from java.lang import *
from java.util import *

class Task(Runnable):
def run(self):
print 'Hello from a Jython task!'

task = Task()
task.run()
result = 'completed successfully'

The only thing that was needed was to add the jython.jar to the JPPF client's classpath.

I also tried to run the task from a Jython script, by defining a Jython class that extends JPPFTask, but I am still facing class loading issues on the node side. I'll keep working on that.
I was hoping it would work as easily as for Groovy or Scala, but it looks like I'll have to dig deeper into this. As I'm completely new to Python/Jython, it may take some time ...

Sincerely,
-Laurent
Logged

georgiuk

  • JPPF Padawan
  • *
  • Posts: 2
Re: execute a jython script
« Reply #2 on: July 15, 2011, 01:10:17 AM »

Thank you for your answer. I haven't tried it yet, but it looks promisisng as a temporary solution. I have faced the same classloading problems, not only with JPPF but with other software libraries too. May be it has something to do with the itnerpreter.

Kostas
Logged
Pages: [1]   Go Up
 
JPPF Powered by SMF 2.0 RC5 | SMF © 2006–2011, Simple Machines LLC Get JPPF at SourceForge.net. Fast, secure and Free Open Source software downloads