0

OK so I want to create an endless loop in Python, the reason is I want to eventually create 4 software pulse with modulated outputs. Because of the nature of PWM is switching in terms of thousands per second I figured I would need 4 threads one for each instance. Now I know I could get some hardware and create hardware PWM outputs, but for testing stuff I want to use software.

to start with I just created the fowling simple snip-it of code. Very simple it creates a single thread and then prints on/off once ever 0.5 seconds.

The issue I found is that it is an endless loop (which is good), but I realised that I cant use Ctrl^c to break out of the loop during testing. To insure that if I need to break out of the code while it is running during testing where would be the best place to put the "try" block, I see that once the main loop exits the ctrl^c is no longer captured.

Thank you

#!/usr/bin/python

import threading
import time

exitFlag = 0

class myThread (threading.Thread):
    def __init__(self, threadID, name, pin):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.pin = pin
    def run(self):
        print "Starting " + self.name
        print_time(self.pin)
        print "Exiting " + self.name

def print_time(pin):
    while True:
         try:
              on = .5
              off = .5
              print  (pin, "on")
              time.sleep(on)
              print (pin, "off")
              time.sleep(off)
         except KeyboardInterrupt:
              break
         except:
              break

# Create new threads
thread1 = myThread(1, "Thread-1", 11)

# Start new Threads
thread1.start()

print "Exiting Main Thread"


    pi@UnderTV ~ $ ./threads2.py
Exiting Main Thread
Starting Thread-1
(11, 'on')
(11, 'off')
(11, 'on')
(11, 'off')
(11, 'on')
(11, 'off')
(11, 'on')
(11, 'off')
^X(11, 'on')
^X(11, 'off')
(11, 'on')
^C(11, 'off')
^C(11, 'on')
^C(11, 'off')
Jaime Soriano
  • 7,309
  • 2
  • 33
  • 45
DevilWAH
  • 2,553
  • 13
  • 41
  • 57
  • 1
    I don't have time to test this at the moment, but it might work if you simple mark your thread `daemon=True` and make a `while True: pass` at the end – Adam Smith Apr 08 '15 at 22:20
  • Hi, Oh that seems to work great :) think I have lots to learn about Python and threading as its not a strength of mine. If you want to post as a solution I be happy to accept it. Cheers – DevilWAH Apr 08 '15 at 22:27
  • 2
    This is a duplicate of [this question](http://stackoverflow.com/questions/19208825/python-2-7-child-thread-not-catching-keyboardinterrupt) – Darth Vader Apr 08 '15 at 22:30
  • Indeed Darth, sorry did not find that when searching. in those comments it says "To be able to stop the other threads, make them check for an event from time to time, and then exit voluntarily." IT would be nice to see an example of a child thread that "check" to insure the parent thread is still running and if not gracefully exit. – DevilWAH Apr 08 '15 at 22:41

0 Answers0