2

this is my first question here. I am trying to run parallel python scripts (that is multiple instances of a same script) from java periodically using the ScheduledThreadPoolExecutor. What i tried to do was using the ProcessBuilder class. To test the concept i have put first script into infinite loop while the second writes something to file and exits. Python scripts i need to make should be identical to each other so i tried to run these two from multiple instaces of a same class that implements runnable.

However the second script never starts. I managed to solve this by creating many classes that have exactly same runnable. But it seems highly impractical to have 10-20 classes that are same. So can i do this somehow within one runnable? Here is the code that shows how i tried to run scripts using the ProcessBuilder:

public class TestScripts{

public static void main(String[] args){
       ScheduledThreadPoolExecutor threadPool = new ScheduledThreadPoolExecutor(2);
       threadPool.scheduleAtFixedRate(new MyTask1(), 1,2, TimeUnit.SECONDS);
       threadPool.scheduleAtFixedRate(new MyTask1(), 1,2, TimeUnit.SECONDS);
        }
  }


class MyTask1 implements Runnable{
public void run(){
     System.out.println("Task1 is running");
     ProcessBuilder processBuilder = new ProcessBuilder("C:\\Python27\\python.exe",
                                                        "C:\\Python27\\test.py");
     ProcessBuilder processBuilder2 = new ProcessBuilder("C:\\Python27\\python.exe",
                                                           "C:\\Python27\\test2.py");
    processBuilder.redirectOutput(Redirect.INHERIT);

    try {
        Process process = processBuilder.start();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    try {
        Process process2 = processBuilder.start();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
}

1 Answer 1

1

There is a typo in the line starting 2nd process:

Process process2 = processBuilder.start();

should be of course:

Process process2 = processBuilder2.start();

Besides you are scheduling 2 tasks, where each task starts 2 processes. So each 2 seconds there are 4 processes started (2x test.py, 2x test2.py). If I understand correctly what you're trying to do, scheduling only one MyTask1 should be enough.

Sign up to request clarification or add additional context in comments.

1 Comment

Ohh, that was exactly the cause of problem. didn't notice the typo! I needed to schedule n number of worker threads from the thread pool where each thread runs same script few times before next period. Thank you sir

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.