python 处理signal kill -15

import signal
import time
import os
class GracefulKiller:
kill_now = False
netcount = 2
def init(self):
signal.signal(signal.SIGTERM, self.exit_gracefully)

def exit_gracefully(self,signum, frame):
self.kill_now = True

def exit_while(self):
net2count = 2
num = 1
while net2count > 1:
time.sleep(1)
print("doing something in a loop ...")
print(os.getpid())
if self.kill_now:
while num < 100:
time.sleep(1)
num = num + 1
print("End of the program. I was killed gracefully")
net2count=1
self.netcount=1

if name == 'main':
killer = GracefulKiller()
while killer.netcount > 1:
if not killer.kill_now:
killer.exit_while()

while not killer.kill_now:

time.sleep(1)

print("doing something in a loop ...")

print(os.getpid())

posted @ 2021-10-29 10:30  一晃而过的少年  阅读(433)  评论(0编辑  收藏  举报