Python MQTT Coding Examples

Stopping a Script

I use two methods in my scripts the first using a try and except block to catch the keyboard interrupt the second uses the signal module :

try:
  while True:
    time.sleep(1)
  except KeyboardInterrupt:
    print("interrupted by keyboard")

Using the signal module

import signal
import os
import time
import sys
def receiveSignal(signalNumber, frame):

    print('Received:', signalNumber)
    sys.exit()
signal.signal(signal.SIGINT, receiveSignal)
while runflag:
    print('Waiting...')
    time.sleep(3)

If you haven’t used the signal module before this tutorial should be of use.

Please rate? And use Comments to let me know more
[Total: 0   Average: 0/5]