Note that there are some explanatory texts on larger screens.

plurals
  1. POTerminating subprocess in python
    text
    copied!<p><strong>Completely rewritten based on my own investigation</strong></p> <p>I have a master script that runs a number of other python scripts. The scripts are created as</p> <pre><code>from subprocess import STDOUT, Popen from signal import SIGINT import shlex p = Popen(shlex.split("python somescript.py arg1 arg2"), cwd="../src/somedir", stderr=STDOUT) </code></pre> <p>And are terminated with</p> <pre><code>p.send_signal(SIGINT) p.wait() </code></pre> <p>Inside them there is the following code</p> <pre><code>if __name__ == "__main__": import signal def terminate(*args): raise KeyboardInterrupt signal.signal(signal.SIGINT, terminate) # do some work here </code></pre> <p>Each script has some function having</p> <pre><code>try: # create workers except KeyboardInterrupt: # cleanup, wait for the current worker to end, then return </code></pre> <p>All of the described works as supposed - the master script creates the processes, when it's execution ends, it sends them SIGINT, they handle it in the correct way, gracefully exiting.</p> <p>Now, I want to run the Django dev server in the same way.</p> <p>I altered the <code>manage.py</code> file:</p> <pre><code>if __name__ == "__main__": import signal def terminate(*args): print 'got SIGINT' raise KeyboardInterrupt signal.signal(signal.SIGINT, terminate) execute_manager(settings) </code></pre> <p>The <code>execute_manager</code> function after a number of calls leads to a django command method having the <code>except KeyboardInterrupt</code> block with <code>sys.exit(0)</code>. So, the whole setup looks the same.</p> <p>The problem: the django server is not actually stopped, though I see the <code>got SIGINT</code> output.</p> <p><strong>Probably an explanation:</strong></p> <p>Looks like django manage.py forks itself, or does something similar; looking into Activity Monitor (the osx's processes explorer) I see 3 python processes started - the one for the master script, and, probably, 2 for manage.py. When terminating, 2 of them stop (the master script, and the one I have the link to with <code>p</code>), while the third remains continuing locking the 8000 port. Is there a way to get process' subprocesses pids?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload