2016-07-09 3 views
-1

특정 PID를 파이썬으로 모니터링하고이 PID가 더 이상 존재하지 않으면 함수를 실행하려고 시도하고 있습니다. 문제는 내 루프가 1,5 (예, 1과 1/2) 루프에서 작동하는 것처럼 보이고 자기 자신을 깨뜨리는 것입니다.while Python의 True 문제

while True: 
    print "[DEBUG] We are in the loop" 
    query = "Select * from Win32_Process where ProcessId = " + str(monitorPID) 
    if (GetObject('winmgmts:').ExecQuery(query).count == 0): 
     RunTheProgramAgain() 
    print "[DEBUG] Current PID is %d - ProcNAME is %s" % (monitorPID, procName) 
    time.sleep(5) 

그것은 성공 프로그램을 다시 실행, 그것은뿐만 아니라 프로그램의 PID를 업데이트하지만 두 번째 루프가 발생할 때, 그것은 마지막 디버그 인쇄하기 전에, 아무 이유없이 중단. 어떤 도움을 주시면 감사하겠습니다.

+1

'RunTheProgramAgain()'과'ExecQuery()'가하는 것을 보여주십시오. – Will

+0

닫는 다른 괄호가 누락 되었습니까? 또한 Will과 동의하십시오. 더 많은 코드를 보여줘야합니다. – Malcriado415

+0

@JulienBernu 내 코드가 형식화되고 들여 쓰기가 올바르다. – CDoc

답변

1

코드를 try 블록에 배치하고 예외가 throw되는지 확인하십시오.

 
while True: 
    try: 
     print "[DEBUG] We are in the loop" 
     if (GetObject('winmgmts:').ExecQuery("Select * from Win32_Process where ProcessId = " + str(monitorPID)).count == 0): 
      RunTheProgramAgain() 
     print "[DEBUG] Current PID is %d - ProcNAME is %s" % (monitorPID, procName) 
     time.sleep(5) 
    except Exception as e: 
     print e 
     pass 
관련 문제