从子线程python中杀死主线程

从子线程python中杀死主线程,python,windows,Python,Windows,我想在函数check_Temp退出/引发异常时终止主进程。我知道这可能不是最好的方法,但我必须集成很多代码,这要容易得多。另外,我确实想要一个硬关机,所以在关闭程序后,如果出现一些错误,这并不重要 我尝试了os.taskskill()、sys.exit()。但是子进程不会杀死主进程。我不介意所有python进程都被杀死。 psutil下载是我公司的IT部门提供的防火墙,所以我想知道是否有人有其他解决方案 import threading import time import os from su

我想在函数check_Temp退出/引发异常时终止主进程。我知道这可能不是最好的方法,但我必须集成很多代码,这要容易得多。另外,我确实想要一个硬关机,所以在关闭程序后,如果出现一些错误,这并不重要

我尝试了os.taskskill()、sys.exit()。但是子进程不会杀死主进程。我不介意所有python进程都被杀死。 psutil下载是我公司的IT部门提供的防火墙,所以我想知道是否有人有其他解决方案

import threading
import time
import os
from subprocess import call  
#import psutil

def check_Temp(temp, delay, run_event,pid):
    while run_event.is_set(): ##code for checking temperature will go here.
        time.sleep(delay)
        print "temp is %s \n"%temp
        temp=temp-1
        #print os.getpid()
        if temp<=38:
            raise Exception('LowTemp')
            #call('pkill python', shell=True)  
            os.popen('TASKKILL /PID '+str(pid)+' /F')
            #os.killall()
            #sys.exit() #want to exit main loop here

if __name__ == "__main__":
    run_event = threading.Event()
    run_event.set()
    temp =45
    d1=.1
    #print os.getpid()
    pid=os.getpid();
    t1 = threading.Thread(target = check_Temp, args = (temp,d1,run_event,pid))
    t1.start()
    print "Starting"

 ########## main code will go here, just have a simple counter here to test the functionality.

    x=25
    try:
        while 1:
            time.sleep(.1)
            x=x-1 
            print "x is %s \n"%x
            if x<0:
                print "x is %s"%x
                raise Exception('spam', 'eggs')
            #exit()
    except Exception as e:
        print e     # the exception instance
        run_event.clear()
        t1.join()
        print "thread successfully closed"

诀窍是使用标志和回调而不是异常

import threading
import time
import os
from subprocess import call

def check_Temp(temp, delay, run_event,pid, endit):
    while run_event.is_set():
        time.sleep(delay)
        ##code for checking temperature will go here.
        print "temp is %s \n"%temp
        temp=temp-1
        #print os.getpid()
        if temp<=38:
            print 'LowTemp %s!' % (temp, )
            endit()
            run_event.clear()

if __name__ == "__main__":
    run_ok = True
    def Terminator():
        global run_ok
        print "Terminating"
        run_ok = False

    run_event = threading.Event()
    run_event.set()
    temp =45
    d1=.1
    #print os.getpid()
    pid=os.getpid();
    run_ok = True
    t1 = threading.Thread(target = check_Temp, args = (temp,d1,run_event,pid, Terminator))
    t1.start()
    print "Starting"

 ########## main code will go here, just have a simple counter here to test the functionality.

    x=25
    try:
        while run_ok:
            time.sleep(.1)
            x=x-1
            print "x is %s "%x
            if x<0:
                print "x is %s"%x
                raise Exception('spam', 'eggs')
            #exit()
        t1.join()
        print 'Finished!'
导入线程
导入时间
导入操作系统
从子流程导入调用
def检查温度(温度、延迟、运行事件、pid、结束):
当运行\u事件时。是否设置了\u()
时间。睡眠(延迟)
##检查温度的代码将显示在这里。
打印“温度为%s\n”%temp
温度=温度-1
#打印os.getpid()
中频温度
import threading
import time
import os
from subprocess import call

def check_Temp(temp, delay, run_event,pid, endit):
    while run_event.is_set():
        time.sleep(delay)
        ##code for checking temperature will go here.
        print "temp is %s \n"%temp
        temp=temp-1
        #print os.getpid()
        if temp<=38:
            print 'LowTemp %s!' % (temp, )
            endit()
            run_event.clear()

if __name__ == "__main__":
    run_ok = True
    def Terminator():
        global run_ok
        print "Terminating"
        run_ok = False

    run_event = threading.Event()
    run_event.set()
    temp =45
    d1=.1
    #print os.getpid()
    pid=os.getpid();
    run_ok = True
    t1 = threading.Thread(target = check_Temp, args = (temp,d1,run_event,pid, Terminator))
    t1.start()
    print "Starting"

 ########## main code will go here, just have a simple counter here to test the functionality.

    x=25
    try:
        while run_ok:
            time.sleep(.1)
            x=x-1
            print "x is %s "%x
            if x<0:
                print "x is %s"%x
                raise Exception('spam', 'eggs')
            #exit()
        t1.join()
        print 'Finished!'