3

I am trying to open and close an application sequentially. But the problem is the application is being opened but to enter to the next line which is the closing line of that application I have to manually close the application.

import os
os.system("scad3 file.txt")
os.system("TASKKILL /PID scad3.exe /T")

scad3 is the application i wish to run,but to enter the next line i.e., taskkilling line, I have to manually close the window please let me know is there any way to solve it??

thank you very much in advance

user3582828
  • 31
  • 1
  • 1
  • 2

2 Answers2

5

I guess os.system is a blocking call. Try using the Popen Objects in python:-

import subprocess
p = subprocess.Popen("notepad.exe")
p.terminate()

Refer :https://docs.python.org/2/library/subprocess.html#popen-objects

Anjali
  • 245
  • 3
  • 12
  • 3
    There is no guess. 1st sentence of the [`documentation`](https://docs.python.org/2/library/subprocess.html#subprocess.call) for `subprocess.call` reads "Run the command described by args. **Wait for command to complete**, then return the returncode attribute." – luk32 Apr 28 '14 at 20:35
  • It would be helpful for the OP to use actual program args in the answer: `Popen(["scad3.exe", "file.txt"])`. Add `rc = p.wait()` after `p.terminate()`, to wait until the program closes. – jfs Apr 28 '14 at 23:59
  • the line p.terminate() is working but the problem is the application is running without considering about the loop below it. – user3582828 Apr 29 '14 at 05:52
  • I have used terminate() and kill()methods of Popen in the past and it has worked for me. Are there any conditions to enter this loop? Without the details is it difficult.. May be post a new question since now the issue is different? – Anjali Apr 29 '14 at 06:02
  • the whole thing is working with a small additional line i.e., time.sleep() between Popen() and terminate().Thank you very much the solution. – user3582828 Apr 29 '14 at 06:16
2

You can try using popen to execute command then wait given time and try to get result or kill the subrocess if it hasn't finished.

import subprocess

def get_array_from_cmd_str(cmd_str):
  cmd_str_parts = cmd_str.split(" ")
  return [cmd_part for cmd_part in cmd_str_parts]

def run_command_str(command):
  p = subprocess.Popen(get_array_from_cmd_str(command),
                      stdout = subprocess.PIPE, stderr = subprocess.PIPE).communicate()[0]
  resp = {'out': p[0],
          'err': p[1]}
  return resp

to run a command use the "run_command_str" function above in this way:

import time

cmd = "scad3 file.txt"
cmd_out = run_command_str(cmd)
expected_execution_time = 5
time.sleep(expected_execution_time)
if cmd_out['err'] != '':
  pass  # handle error here

Now if your program does not close automatically you can modify the approach to manually kill it using methods descriged in this thread.

(examples not tested on Windows)

EDIT: Code modified according to valuable comment. Example makes a blocking call and does not address the issue; use other ones.

Community
  • 1
  • 1
Mr. Girgitt
  • 2,853
  • 1
  • 19
  • 22
  • OP asks for a way to start a program without waiting for it to finish. `.communicate()` waits for the command to exit. On Windows, you can always pass the command as a string (it is a native interface (CreateProcess)). Splitting on whitespace is fragile. Drop unnecessary `str()` call that might fail even for valid commands. `scad3` might be a GUI program that has no meaningful stdout/stderr. Use `!=` instead of `is not` to compare strings. `is` compare objects identities, not their values. You could use a named tuple instead of returning the dict. – jfs Apr 29 '14 at 00:09
  • Thank you for your comment. I agree this apprach is not valid in general and works for specific cases only. Other answers may be more relevant. Does named tuple have any advantage over dict? – Mr. Girgitt Apr 29 '14 at 10:37
  • You can't just remove `.communicate()` and expect everything to continue to work e.g., `Popen()` and `.communicate()` have different return values. Named tuple is like an ordinary tuple but its items have names. It is more lightweight than dict. Its usage is optional – jfs Apr 29 '14 at 10:46
  • thank you very much since the sleep() helped me to get out of my problem. – user3582828 Apr 29 '14 at 10:46