Stoping python script using KeyboardInterrupt - python

Hi why is my KeyboardInterrupt: is not stoping my program when i hit control c or control x? this is my current code.
I am using python Threading that runs 2 function coinPulser and coinPulserDone.
import threading
import time
lock = threading.Lock()
counter = 0
input = 3
def coinPulser ():
global counter
global input
lock.acquire()
try:
while counter < input:
counter+=1
time.sleep(.1)
if counter in [1,3,5]:
print(counter)
return counter
finally:
lock.release()
def coinPulserDone ():
while True:
print(coinPulser())
try:
coinpulser = threading.Thread(target = coinPulser)
coinpulser.start()
coinpulserdone = threading.Thread(target = coinPulserDone)
coinpulserdone.start()
except KeyboardInterrupt:
coinpulser.stop()
coinpulserdone.stop()
print('Thread Stops')

I suspect the problem is that your code exits your try/except block before you press Cntr-C. You need to add some form of a loop that will hold it in that block. A simple loop such as
while True:
time.sleep(1)
before your except line should do the trick.

Related

KeyboardInterrupt not working in Python to stop thread

I want to capture and store the images from live streams using two threads. I want to stop the threads with Ctrl + C keyboard interrupt. I am calling a function from
if name == "main":
but it is not working. I am using spyder with an anaconda environment. I also tried it using terminal but it is not working. Can anyone explain why and how it will work?
The code is as follow
class ImageGrabber(threading.Thread):
def __init__(self, ID):
threading.Thread.__init__(self)
self.ID=ID
self.cam=cv2.VideoCapture(ID)
self.fps = self.cam.get(cv2.CAP_PROP_FPS)
self.w=self.cam.get(cv2.CAP_PROP_FRAME_WIDTH)
self.h=self.cam.get(cv2.CAP_PROP_FRAME_HEIGHT)
(self.grabbed, self.frame) =self.cam.read()
print(f"Camera Opened with fps {self.fps}, width = {self.w}, and height = {self.h}")
plt.imshow(self.frame)
plt.show()
self.stopped = False
def run(self):
global frames
global exitProgram
# print("Entered Grabbed Run ******:")
while not self.stopped:
if not self.grabbed or exitProgram is True:
print("Exit Command reached")
self.stop()
self.cam.release()
else:
(self.grabbed, self.frame) =self.cam.read()
frames.put(self.frame)
def stop(self):
self.stopped = True
class ImageSaver(threading.Thread):
def __init__(self,grabber,codec):
threading.Thread.__init__(self)
global saveImages
fourcc = cv2.VideoWriter_fourcc(*codec)
self.fName=self.getDateStamp() + '.avi'
if saveImages is False:
self.out = cv2.VideoWriter(self.fName, fourcc,grabber.fps ,(int(grabber.w),int(grabber.h)))
def getDateStamp(self):
filedate = str(datetime.now())
filedate = filedate[0:-7]
filedate = filedate.replace(':', '_')
filename = filedate
#print(filename)
return filename
def run(self):
global frames
global saveImages
while True:
# print("Entered Loop 1:")
if(not frames.empty()):
# print("Entered frame showing:")
self.Currframe=frames.get()
if saveImages is False:
self.out.write(self.Currframe)
else:
self.imgName=self.getDateStamp() + '.tiff'
cv2.imwrite(self.imgName,self.Currframe)
print(f"Queue Size in writing = {frames.qsize()}")
# print("Show Image")
elif exitProgram is True:
print("Exit Command Main reached")
print(f"Final Queue Size at exit = {frames.qsize()}")
break
def initStart(*args):
argsList=sum(list(args),[])
argsLen = len(argsList)
if(argsLen==2):
ID = int(argsList[0])
codec = argsList[1]
elif(argsLen==1):
ID = int(argsList[0])
codec = 'XVID'
else:
ID = 0
codec = 'XVID'
grabber = ImageGrabber(ID)
saver = ImageSaver(grabber,codec)
grabber.start()
saver.start()
e = threading.Event()
# main.join()
# grabber.join()
print ('Press CTRL-C to interrupt')
while grabber.isAlive():
try:
time.sleep(1) #wait 1 second, then go back and ask if thread is still alive
print ('Sleep...')
except: #if ctrl-C is pressed within that second,
#catch the KeyboardInterrupt exception
e.set() #set the flag that will kill the thread when it has finished
exitProgram=True
print ('Exiting...')
grabber.join()
saver.join()
if __name__ == "__main__":
full_cmd_arguments = sys.argv
# Keep all but the first. First contains the path of script or file
argument_list = full_cmd_arguments[1:]
initStart(argument_list)
If I change the program in such a way that keyboardInterrupt is called without any function then it works
grabber = ImageGrabber(0)
main = Main(grabber,'XVID')
grabber.start()
main.start()
e = threading.Event()
print ('Press CTRL-C to interrupt')
while grabber.isAlive():
try: time.sleep(1) #wait 1 second, then go back and ask if thread is
except KeyboardInterrupt: #if ctrl-C is pressed within that second,
#catch the KeyboardInterrupt exception
e.set() #set the flag that will kill the thread when it has finished
exitProgram=True
print ('Exiting...')
grabber.join()
main.join()
The above code is called directly as python script then it works. Any reason?
Any idea? how it can work?
...
print ('Press CTRL-C to interrupt')
try:
while grabber.isAlive():
time.sleep(1) #wait 1 second, then go back and ask if thread is
except KeyboardInterrupt: #if ctrl-C is pressed within that second,
#catch the KeyboardInterrupt exception
e.set()
...
Try this way.
The reason why KeyboardInterrupt was not working is because the while loop was scoping the try except statement, so it would repeat it continuously even though you were pressing the KeyboardInterrupt.
Hope this helps!

Why does terminate() of multiprocessing.pool.Threadpool hang?

I want to stop asynchronous multiprocessing jobs with KeyboardInterrupt. But sometimes hang occurred when call terminate.
from multiprocessing.pool import ThreadPool
import multiprocessing
import time
import queue
import inspect
def worker(index):
print('{}: start'.format(index))
for i in range(5):
time.sleep(1)
print('{}: stop'.format(index))
return index, True
def wrapper(index, stopEvent, qResult):
if stopEvent.is_set() is True:
return index, False
try:
result = worker(index)
except:
print('*' * 50)
return index, False
else:
if result[1] == True:
qResult.put(result)
return result
def watcher(qResult, stopEvent):
cntQ = 0
while True:
try:
result = qResult.get(timeout=10)
qResult.task_done()
except queue.Empty:
if stopEvent.is_set() is True:
break
except KeyboardInterrupt:
stopEvent.set()
else:
cntQ += 1
print(result)
qResult.join()
qResult.close()
print('qResult count:', cntQ)
def main():
stopEvent = multiprocessing.Event()
qResult = multiprocessing.JoinableQueue()
qResult.cancel_join_thread()
watch = multiprocessing.Process(target=watcher, args=(qResult, stopEvent))
watch.start()
pool = ThreadPool()
lsRet = []
for i in range(100000):
try:
ret = pool.apply_async(wrapper, args=(i, stopEvent, qResult))
lsRet.append(ret)
except KeyboardInterrupt:
stopEvent.set()
time.sleep(1)
break
if i+1 % 10 == 0:
time.sleep(2)
cntTotal = len(lsRet)
cntRet = 0
for ret in lsRet:
if stopEvent.is_set():
break
try:
ret.get()
except KeyboardInterrupt:
stopEvent.set()
time.sleep(1)
else:
cntRet += 1
if stopEvent.is_set() is False:
stopEvent.set()
print(inspect.stack()[0][1:4])
if watch.is_alive() is True:
watch.join()
print(inspect.stack()[0][1:4])
pool.terminate() # Why hang??????????
print(inspect.stack()[0][1:4])
pool.join()
print(cntTotal, cntRet)
if __name__ == '__main__':
main()
main() invokes a watcher() thread and many wrapper() threads asynchronously using multiprocessing.pool.Threadpool.
wrapper() calls worker() and put its result to queue.
watcher() watches above queue of results.
If ctrl-c pressed, stopEvent is set.
When stopEvent is set, wrapper() stops calling worker(), and Watcher() indicates queue.Empty and stopEvent and exits loop.
Finally main() calls terminate() of pool.
Sometimes processes done well, but sometimes hang. It's different each time.
You should put the code in try except block and catch a built-in exception KeyboardInterrupt see the example here Capture keyboardinterrupt

Daemon thread not exiting despite main program finishing

I've already referred to this thread, but it seems to be outdated
and there doesn't seem to be a clean explanation
Python daemon thread does not exit when parent thread exits
I'm running python 3.6 and trying to run the script from either IDLE or Spyder IDE.
Here is my code:
import threading
import time
total = 4
def creates_items():
global total
for i in range(10):
time.sleep(2)
print('added item')
total += 1
print('creation is done')
def creates_items_2():
global total
for i in range(7):
time.sleep(1)
print('added item')
total += 1
print('creation is done')
def limits_items():
#print('finished sleeping')
global total
while True:
if total > 5:
print ('overload')
total -= 3
print('subtracted 3')
else:
time.sleep(1)
print('waiting')
limitor = threading.Thread(target = limits_items, daemon = True)
creator1 = threading.Thread(target = creates_items)
creator2 = threading.Thread(target = creates_items_2)
print(limitor.isDaemon())
creator1.start()
creator2.start()
limitor.start()
creator1.join()
creator2.join()
print('our ending value of total is' , total)
limitor thread doesn't seem to be ending despite being a daemon thread.
Is this a way to get this working from IDLE or Spyder?
Thanks.
I had the same Problem and solved it by using multiprocessing instead of threading:
from multiprocessing import Process
import multiprocessing
from time import sleep
def daemon_thread():
for _ in range(10):
sleep(1)
print("Daemon")
if __name__ == '__main__':
multiprocessing.freeze_support()
sub_process = Process(target = daemon_thread, daemon = True)
sub_process.start()
print("Exiting Main")
I haven't yet really understood why I need the call to freeze_support() but it makes the code work.

Python Tkinter, Stop a threading function

I'm currently developing a GUI for a 3D printer and I'm having a problem of how to stop a threading function. I want to be able to click a button that has another function within my GUI that will stop the threading function from sending strings of G-code across the serial port. Currently the function has threading incorporated to allow other functions to be triggered during printing. I would greatly appreciate some advice on how I would incorporate this stop feature.
Below is the function that opens a G-code file and sends each line across the serial port.
def printFile():
def callback():
f = open(entryBox2.get(), 'r');
for line in f:
l = line.strip('\r')
ser.write("<" + l + ">")
while True:
response = ser.read()
if (response == 'a'):
break
t = threading.Thread(target=callback)
t.start()
Threads cannot be stopped, they have to stop themselves. So you need to send a signal to the thread that it's time to stop. This is usually done with an Event.
stop_event = threading.Event()
def callback():
f = open(entryBox2.get(), 'r');
for line in f:
l = line.strip('\r')
ser.write("<" + l + ">")
while True:
response = ser.read()
if (response == 'a'):
break
if stop_event.is_set():
break
t = threading.Thread(target=callback)
t.start()
Now if you set the event elsewhere in your code:
stop_event.set()
The thread will notice that, break the loop and die.
Use a global variable as a condition for the thread to stop.
send_gcode = True
def printFile():
def print_thread():
f = open(entryBox2.get(), 'r');
for line in f:
if not send_gcode:
break
l = line.strip('\r')
ser.write("<" + l + ">")
while True:
response = ser.read()
if (response == 'a'):
break
t = threading.Thread(target=print_thread)
send_gcode = True
t.start()
The thread will run until send_gcode is set to False (by e.g. a callback for a button:
def stop_callback(event):
global send_gcode
send_gcode = False

is there a way to put a thread into sleep from outside of the thread?

import time
import threading
def do_something():
while True:
time.sleep(0.5)
print('I am alive')
def main():
while True:
time.sleep(1)
print('Hello')
daemon_thread = threading.Thread(target=do_something, daemon=True)
daemon_thread.start()
main()
Is there a way I be able to put daemon_thread to sleep for example for 3 seconds from outside of do_something()? I mean something hypothetical like daemon_thread.sleep(3)?
Create a counter for half seconds and then make the sleep function increment that counter:
lock = Lock()
counter = 0
def do_something():
global counter
while True:
time.sleep(0.5)
with lock:
if counter == 0:
print('I am alive')
else:
counter -= 1
def increment(seconds):
global counter
with lock:
counter += 2*seconds
# after starting thread
increment(3) # make the thread wait three seconds before continuing

Categories

Resources