DeaD_EyE

threading - sigint

Jan 14th, 2020
1,103
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2. import signal
  3. import threading
  4.  
  5.  
  6. class Server(threading.Thread):
  7.     def __init__(self, *args, **kwargs):
  8.         self._active = False
  9.         super().__init__(*args, **kwargs)
  10.  
  11.     def run(self):
  12.         self._active = True
  13.         while self._active:
  14.             time.sleep(1)
  15.  
  16.     def stop(self):
  17.         print('Stopping process...')
  18.         self._active = False
  19.  
  20.  
  21. def stop_server(sig, frame):
  22.     server.stop()
  23.  
  24.  
  25. if __name__ == '__main__':
  26.     server = Server()
  27.     server.start()
  28.     signal.signal(signal.SIGINT, stop_server)
  29.     print('Stop Thread with CTRL+C')
RAW Paste Data