October 20th, 2004, 03:57 PM
Killing command windows opened with os.system
I am running a python script that loads a command line based program. The command line based program is showing a propensity to stall.
I would like to be able to have python wait a few seconds and then kill the command line program and go to the next step.
I have searched around but I cannot find the proper words if this has been discussed.
October 21st, 2004, 03:59 AM
What you want as is not difficult but is platform dependent and you will probably have to make changes to the way you launch programs.
os.system does not provide any process info or access to the application IO so it is very difficult to control. If you can get the process ID then it is possible to kill the process (see below).
Other process launching methods like the popen2.Popen4 class or the os.spawnXX functions provide differing levels of process control. On *nix this will allow you to use the os.kill(processID,signal) function.
If it is a Windows environment then You need to use WIN32 api's to do termination:
I hope this helped.
October 21st, 2004, 08:33 AM
I believe this is a more platform independant solution, although I'm not quite sure i've only been pythoning a week.
Originally Posted by Grim Archon
I still have this issue with the launcher replying slowly when the thread is running, however my thread launches its own threads, and it's a high memory high cpu-time application.
thread_name = threading.Thread \
(group, target, name, args, kwargs)
(sleep or what have you)
"I have no use for your logic now, I'm programming"
October 21st, 2004, 09:39 AM
It's true that if the program that is being run is another python module it should be possible to run it as a thread of the controlling program. However, I can't find a shutdown method in the threading documentation or source.
The problem with threads is that you can only ask them nicely to terminate (Unless you know some under the hood code???).
The new sub process module found in 2.4 might be useful here.
October 21st, 2004, 10:28 AM
<looks a bit deeper at his code>
You're right. The .shutdown() method on my 'thread' was actually on a class surrounding the thread. The method went into the thread's function and set a shutdown variable.
Sorry for the confusion.
October 21st, 2004, 10:46 AM
No probs here