2

I want to run some python script in my application, but sometimes I want to terminate the script before it finishes and then run some other python script.

I currently use PyRun_SimpleString(python_script) to run the script, but I don't know how to terminate it. Can somebody help me?

Fent
  • 31
  • 3
  • When do you want to stopthe script? How do you decide to stop it? Before calling PyRun_SimpleString the script has not been started yet, and after calling PyRun_SimpleString it has already finished. – n. m. could be an AI Nov 22 '16 at 15:54
  • In my Application, select a python script file, and read it's content ,and then call the PyRun_SimpleString() to run it , But sometime,user can stop this script by press a button STOP to terminate the script before it finishes,i don't know how to terminate it. If use the pytho command line to run script ,we can use CTRL+C to break ,but how to use the python.dll API to break it ,not find yet – Fent Nov 23 '16 at 00:31
  • @n.m. It's not guaranteed that all threads exits before PyRun_SimpleString is returned. Sometimes you have to wait till the python interpreter terminates all threads. I have observed this behavior in old XBMC source code python interface. – Pulathisi Bandara Nov 22 '17 at 03:46
  • @Fent what you can try is raising PyExc_SystemExit which will achieve what you may want. But you may have to raise this exception for each thread if you're having many threads. – Pulathisi Bandara Nov 22 '17 at 04:04
  • 1
    @Fent could your script run in a subprocess? – georgexsh Jan 26 '18 at 09:26
  • 1
    Do you want this in C because this can easily be done in Python? – Xantium Jan 26 '18 at 14:04
  • `PyRun_SimpleString` is a synchronous function, So I assume only way to terminate would be to run it in a thread and abort the thread – Tarun Lalwani Jan 27 '18 at 05:46
  • @PulathisiBandara you were interested in this problem - have you seen the solution in this duplicate suggestion? (I think it's definitely worth keeping the answer on this question though) – DavidW Jan 28 '18 at 18:10
  • @DavidW Thank you for attaching the duplicate. I saw some answers similar to the attached duplicate, but not complete like the attached one here, thanks. I agree with you the question is the same, though the answers are different in the approach. – Pulathisi Bandara Jan 29 '18 at 06:14

1 Answers1

-1

To kill a python script from your C program, you can find its process id, using the name of the python script(or some other regex), and then kill the process.

My C is a little rusty, but the code below will kill a running python script called test.py.

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int main (void) {
    bool kill_python = true;

    if (kill_python == true) {
        FILE *cmd = popen("kill -9 $(ps aux | grep '[t]est.py' | awk '{print $2}')", "r");
        pclose(cmd);
    }

    return 0;
}

ps aux | grep '[t]est.py' | awk '{print $2}' returns the process id of the python script kill -9 {PID} kills the process forcibly(-9 = non-catchable, non-ignorable kill)

grizzthedj
  • 7,131
  • 16
  • 42
  • 62
  • This answer itself doesn't solve the problem, But I have an idea going through this solution. Please consider this to the question asked, which is not a 'python script' he wants to stop but a 'python script string' which currently doesn't have a PID. What he can do is fork a process, and once he wants to kill the python script, kill that forked process. In that way, I think this is a good approach and hence awarding the bounty. – Pulathisi Bandara Jan 28 '18 at 07:45
  • This answer is already given by @georgexsh, sorry that I didn't notice earlier. – Pulathisi Bandara Jan 28 '18 at 07:48