2016-10-21 6 views
0

2 개의 스크립트가 있습니다.장기 실행 스크립트를 종료 하시겠습니까?

test.py 호출 main.py

import threading 
import time 
import os 

exitflag = 0 

class Testrun(threading.Thread): 
    def __init__(self,threadID,name,delay): 
     threading.Thread.__init__(self) 
     self.threadID = threadID 
     self.name = name 
     self.delay = delay 

    def run(self): 
     print 'launching test '+self.name 
     launch_test(self.name,self.delay) 
     print 'exitiing test '+self.name 

def launch_test(threadname,delay): 
    os.system('test.py') 
    if exitflag ==1: 
     threadname.exit() 

thread = Testrun(1,'test',10) 

thread.start() 

: 나는 test.py를 종료하려면 30 초 지연 후

import time 

while True: 
    print 'hello' 
    time.sleep(1) 

합니다.
나는 파이썬 2.4.2를 사용하고 있습니다. 이벤트 생성 이 코드 나 할 것입니다 다른 대안 모듈에 어떤 제안이 할 수있는 방법이 입니다.

+1

'launch_test (self.name, self.delay)'함수에 너무 많은 인수를 전달한다 :이 게시물을 참조하십시오. 또한 ** main.py **는 ** test.py **를 호출하지 않으며 ** conthello.py **는 무엇입니까? – martineau

답변

0

더 나은 옵션은 외부 프로세스를 실행하기위한 subprocess 모듈 (https://docs.python.org/2/library/subprocess.html)를 사용합니다. How to terminate a python subprocess launched with shell=True

+0

kill() 및 terminate() 메서드는 2.4 버전에 없습니다. 그것들은 doc 당 2.6으로 소개됩니다. 다른 방법은이 경우 문제 – NGB

+0

를 해결하기 위해, 당신은'test.py' 프로세스가'OS를 사용하는'PID = subprocess.Popen ('test.py', 쉘 = 참) .pid' 및 프로세스를 종료를 사용하여 PID를 얻어야한다 .system ("kill -9"+ pid)'(리눅스에서는). Windows를 사용하는 경우,이 조리법을 참조하십시오 http://code.activestate.com/recipes/347462-terminating-a-subprocess-on-windows/ –

관련 문제