2016-10-21 9 views
0

私は2つのスクリプトを持っています。私はtest.pyを終了したい30秒の遅延の後長期実行スクリプトを終了しますか?

import time 

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

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() 


私はPython 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

この場合、 'pid = subprocess.Popen( 'test.py'、shell = True).pid'を使って' test.py'プロセスpidを取得し、 'os .system( "kill -9" + pid) '(Linuxの場合)。 Windowsを使用している場合は、次のレシピを参照してください。http://code.activestate.com/recipes/347462-terminating-a-subprocess-on-windows/ –

関連する問題