7

I'm using a thread to read Strings from a stream (/dev/tty1) while processing other things in the main loop. I would like the Thread to terminate together with the main program when pressing CTRL-C.

   from threading import Thread

   class myReader(Thread):
      def run(self):
         with open('/dev/tty1', encoding='ascii') as myStream:
            for myString in myStream:
               print(myString)
      def quit(self):
         pass # stop reading, close stream, terminate the thread

   myReader = Reader()
   myReader.start()
   while(True):
      try:
         pass # do lots of stuff
      KeyboardInterrupt:
         myReader.quit()
         raise

The usual solution - a boolean variable inside the run() loop - doesn't work here. What's the recommended way to deal with this?

I can just set the Daemon flag, but then I won't be able to use a quit() method which might prove valuable later (to do some clean-up). Any ideas?

abw
  • 209
  • 4
  • 8

2 Answers2

6

AFAIK, there is no built-in mechanism for that in Python 3 (just as in Python 2). Have you tried the proven Python 2 approach with PyThreadState_SetAsyncExc, documented here and here, or the alternative tracing approach here?

Here's a slightly modified version of the PyThreadState_SetAsyncExc approach from above:

import threading
import inspect
import ctypes 
 
def _async_raise(tid, exctype):
    """raises the exception, performs cleanup if needed"""
    if not inspect.isclass(exctype):
        exctype = type(exctype)
    res = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(tid), ctypes.py_object(exctype))
    if res == 0:
        raise ValueError("invalid thread id")
    elif res != 1:
        # """if it returns a number greater than one, you're in trouble, 
        # and you should call it again with exc=NULL to revert the effect"""
        ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
        raise SystemError("PyThreadState_SetAsyncExc failed")
 
def stop_thread(thread):
    _async_raise(thread.ident, SystemExit)
gigabot
  • 66
  • 5
Irfy
  • 9,323
  • 1
  • 45
  • 67
  • I find that I need to use `ctypes.c_long(tid)` otherwise I'm getting `res == 0` – faulty Oct 15 '14 at 03:22
  • I updated the code to use `ctypes.c_long(tid)` instead of just `tid`, now it works as intended. [Fully working gist](https://gist.github.com/Irfy/7e47d49c9a71b529a334cb0ba49a2471). Actually, the async exception may not interrupt the blocking read from the file object, thus becoming equivalent to a manually checked flag that can be set from the main thread -- which is a cleaner solution. If interrupting blocking reads is desired, non-blocking I/O is the way to go. – Irfy Apr 09 '18 at 10:58
4

Make your thread a daemon thread. When all non-daemon threads have exited, the program exits. So when Ctrl-C is passed to your program and the main thread exits, there's no need to explicitly kill the reader.

    myReader = Reader()
    myReader.daemon = True
    myReader.start()
weaver
  • 41
  • 1