할 수 있습니다이 완료되지 않은 경우 다음 명령을 실행 주어진 시간을 기다린 결과를 얻기 위해 시도하거나 subrocess을 죽이고는 popen를 사용해보십시오 . 프로그램이 자동으로 종료되지 않을 경우 수동이의 descriged 방법을 사용하여 죽일 당신이 접근 방식을 수정할 수 있습니다 이제
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
:
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
는 명령이이 방법으로 위의 "run_command_str"기능을 사용하여 실행
this thread.
(Windows에서 테스트하지 예)
편집 : 코드 소중한 의견에 따라 수정. 예는 차단 호출을하고 문제를 해결하지 않습니다. 다른 것들을 사용하십시오.
추측은 없습니다. 'subprocess.call'에 대한 ['documentation'] (https://docs.python.org/2/library/subprocess.html#subprocess.call)의 첫번째 문장은 "args에 기술 된 명령을 실행하십시오. ** Wait ** 명령을 완료하려면 returncode 속성을 반환하십시오. " – luk32
OP에서 실제 프로그램 args를 사용하는 것이 도움이 될 것입니다 :'Popen ([ "scad3.exe", "file.txt"])'. 'p.terminate()'의 뒤에'rc = p.wait()'를 추가해, 프로그램이 닫힐 때까지 대기하십시오. – jfs
p.terminate() 행이 작동하지만 문제는 응용 프로그램이 그 아래의 루프를 고려하지 않고 실행 중입니다. – user3582828