终止长时间运行的脚本?

2024-05-15 14:29:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个剧本。你知道吗

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

它叫测试.py

import time

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

延迟30秒后,我想终止测试.py
我使用的是python2.4.2。 有什么方法可以通过事件生成来实现这一点吗 对此代码或任何其他替代模块的任何建议都可以。你知道吗


Tags: namepytestimportselftimeosdef