0

I have 2 scripts.

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

Which calls test.py:

import time

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

After a delay of 30 secs I want to terminate test.py.
I am using python 2.4.2. Is there any way to do this with event generation Any suggestion to this code or any other alternative module will do.

NGB
  • 400
  • 3
  • 16
  • 1
    `launch_test(self.name,self.delay)` is passing too many arguments to the function. Also **main.py** does not invoke **test.py**, and what is **conthello.py**? – martineau Oct 21 '16 at 13:11

1 Answers1

0

A better option is using subprocess module (https://docs.python.org/2/library/subprocess.html) for launching external processes. See this post also: How to terminate a python subprocess launched with shell=True

Community
  • 1
  • 1
  • kill() and terminate() methods are not there in 2.4 version. They are introduced in 2.6 as per doc. Any other way to solve issue – NGB Oct 25 '16 at 06:57
  • In this case, you should get the `test.py` process pid using `pid = subprocess.Popen('test.py', shell=True).pid` and terminate the process using `os.system("kill -9 "+pid)` (on Linux). If you are using Windows, refer to this recipe: http://code.activestate.com/recipes/347462-terminating-a-subprocess-on-windows/ – Vittorio Palmisano Nov 04 '16 at 09:37