Terminate scripts

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Terminate scripts

Christian Pontesegger

I was wondering if there exists a way to terminate a script running an
endless loop. To clarify the problem I have attached some source code.
None of the tested methods stops the while loop of the main script:

public class PythonTest {

     private static InteractiveInterpreter engine;

     private static final String CODE = "import time\n" +
             "\n" +
             "while True:\n" +
             "      print('still there')\n" +
             "      time.sleep(1)\n" +

     private static final String TERMINATE = "import sys\n" +

     public static void main(String[] args) {

         new Thread(new Runnable() {

             public void run() {
                 long start = System.currentTimeMillis();
                 while (System.currentTimeMillis() - 10 * 1000 < start)

System.out.println(">>>>>>>>>>> Terminating");
//                engine.getSystemState().callExitFunc();
//                engine.getSystemState().exit();
//                engine.interrupt(new

         engine = new InteractiveInterpreter();

         engine.getSystemState().path.insert(0, Py.EmptyString);


         System.out.println("---------- done");

     private static void execute(String input) {
         final PyObject code = Py.compile_command_flags(input, "(none)",
                 CompileMode.exec, new CompilerFlags(), true);

         if (code == Py.None)
             throw new RuntimeException("Could not compile code");

         Py.exec(code, engine.getLocals(), null);


Jython-users mailing list
[hidden email]