#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    2
    Rep Power
    0

    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.

    Thanks,

    Matthew
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Hi,

    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:
    Code:
    import win32api
    win32api.TerminateProcess(processID,0)
    I hope this helped.

    grim
  4. #3
  5. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,399
    Rep Power
    1232
    Originally Posted by Grim Archon
    Hi,

    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.

    grim
    I believe this is a more platform independant solution, although I'm not quite sure i've only been pythoning a week.
    Code:
    import threading
    
    thread_name = threading.Thread \
                         (group, target, name, args, kwargs)
    thread_name.start();
    (sleep or what have you)
    thread_name.shutdown();
    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.

    -Mike

    "I have no use for your logic now, I'm programming"
    - Unknown
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Mike,

    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.

    grim
  8. #5
  9. I <3 ASCII
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Aug 2003
    Posts
    2,399
    Rep Power
    1232
    <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.

    -Mike
  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    No probs here

IMN logo majestic logo threadwatch logo seochat tools logo