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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

5 Comments

There is no guess. 1st sentence of the documentation for subprocess.call reads "Run the command described by args. Wait for command to complete, then return the returncode attribute."
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.
the line p.terminate() is working but the problem is the application is running without considering about the loop below it.
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?
the whole thing is working with a small additional line i.e., time.sleep() between Popen() and terminate().Thank you very much the solution.
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.

4 Comments

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.
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?
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
thank you very much since the sleep() helped me to get out of my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.