Python Exception in thread Thread-1 (most likely raised during interpreter shutdown)? Python Exception in thread Thread-1 (most likely raised during interpreter shutdown)? numpy numpy

Python Exception in thread Thread-1 (most likely raised during interpreter shutdown)?


This is pretty common when using daemon threads. Why are you setting .daemon = True on your threads? Think about it. While there are legitimate uses for daemon threads, most times a programmer does it because they're confused, as in "I don't know how to shut my threads down cleanly, and the program will freeze on exit if I don't, so I know! I'll say they're daemon threads. Then the interpreter won't wait for them to terminate when it exits. Problem solved."

But it isn't solved - it usually just creates other problems. In particular, the daemon threads keep on running while the interpreter is - on exit - destroying itself. Modules are destroyed, stdin and stdout and stderr are destroyed, etc etc. All sorts of things can go wrong in daemon threads then, as the stuff they try to access is annihilated.

The specific message you're seeing is produced when an exception is raised in some thread, but interpreter destruction has gotten so far that even the sys module no longer contains anything usable. The threading implementation retains a reference to sys.stderr internally so that it can tell you something then (specifically, the exact message you're seeing), but too much of the interpreter has been destroyed to tell you anything else about what went wrong.

So find a way to shut down your threads cleanly instead (and remove .daemon = True). Don't know enough about your problem to suggest a specific way, but you'll think of something ;-)

BTW, I'd suggest removing the maxsize=0 arguments on your Queue() constructors. The default is "unbounded", and "everyone knows that", while few people know that maxsize=0 also means "unbounded". That's gotten worse as other datatypes have taken maxsize=0 to mean "maximum size really is 0" (the best example of that is collections.deque); but "no argument means unbounded" is still universally true.


(1) This works for me.SOURCE: https://realpython.com/intro-to-python-threading/#starting-a-thread

(2) I use daemon

import loggingimport threadingimport timedef thread_function(name):    logging.info("Thread %s: starting", name)    time.sleep(2)    logging.info("Thread %s: finishing", name)if __name__ == "__main__":    format = "%(asctime)s: %(message)s"    logging.basicConfig(format=format, level=logging.INFO,                        datefmt="%H:%M:%S")    threads = list()    for index in range(3):        logging.info("Main    : create and start thread %d.", index)        x = threading.Thread(target=thread_function, args=(index,) ,  daemon=True)        threads.append(x)        x.start()    for index, thread in enumerate(threads):        logging.info("Main    : before joining thread %d.", index)        thread.join()        logging.info("Main    : thread %d done", index)