小試了一下怎樣用Python開一隻thread, 然後讓它收signal 做reload跟 shutdown
https://github.com/jofenting/memo/blob/master/testSig.py
跑起來
用另一個console送signal給他
reload
shutdown
https://github.com/jofenting/memo/blob/master/testSig.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import threading | |
import time | |
import signal | |
import os | |
class TestThread(threading.Thread): | |
__terminate = False | |
__reload = False | |
__thisThread = None | |
def __init__(self): | |
threading.Thread.__init__(self) | |
print 'init' | |
def __del__ (self) : | |
print 'del' | |
def run(self): | |
self.__thisThread = threading.currentThread() | |
print 'self.__thisThread=%s' % self.__thisThread | |
while True: | |
if self.__reload: | |
print 'reload' | |
self.__reload = False | |
if self.__terminate: | |
print 'terminate' | |
break | |
print '.' | |
time.sleep(1) | |
def reload(self): | |
self.__reload = True | |
def stop(self): | |
self.__terminate = True | |
def isRunning(self): | |
return not self.__terminate | |
def sighandler(self, signum, stack): | |
print 'stgnum=%s' % signum | |
if signum == signal.SIGHUP: | |
self.reload() | |
if signum == signal.SIGTERM: | |
self.stop() | |
if __name__ == '__main__': | |
print 'My PID is:', os.getpid() | |
thread = TestThread() | |
signal.signal(signal.SIGHUP, thread.sighandler) | |
signal.signal(signal.SIGTERM, thread.sighandler) | |
thread.start() | |
while thread.isRunning(): | |
print('-') | |
time.sleep(5) | |
print 'join' | |
thread.join() |
跑起來
python testSig.py |
用另一個console送signal給他
reload
kill -1 <pid> |
shutdown
kill -15 <pid> |
收工
留言
張貼留言