Daemon parameter in threading.Thread() function isn't working while code is executing in a .ipynb file

Here is the code.

import logging
import threading
import time

def thread_function(name):
    logging.info("Thread %s: starting", name)
    time.sleep(5)
    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")

    logging.info("Main    : before creating thread")
    x = threading.Thread(target=thread_function, args=(1,), daemon=True)
    logging.info("Main    : before running thread")
    x.start()
    logging.info("Main    : wait for the thread to finish")
    # x.join()
    logging.info("Main    : all done")

The output is

08:31:35: Main : before creating thread
08:31:35: Main : before running thread
08:31:35: Thread 1: starting
08:31:35: Main : wait for the thread to finish
08:31:35: Main : all done
08:31:45: Thread 1: finishing

Where are you calling the ‘thread_function’ in your code?
i.e.,

thread_function('some name')

That happens as part of the thread spawning. That’s not a problem.

I do notice, though, that you’re not joining the thread here. So my guess is that you’re expecting the other thread to continue, but it’s not? That’s what daemon=True does - it won’t continue once the main thread (and any other non-daemon threads) finishes. Are you sure you want that option?

The main thread finishes, but the thread of function “thread_function” seem continue in my code when running as a ipynb file.

The output is

08:31:35: Main : before creating thread
08:31:35: Main : before running thread
08:31:35: Thread 1: starting
08:31:35: Main : wait for the thread to finish
08:31:35: Main : all done
08:31:45: Thread 1: finishing

Okay, this is VERY confusing. You have edited your posts in a way that completely changes the meaning. What exactly is happening, what do you expect to happen, and what do you want to happen?

Please, don’t edit your posts in this way. Respond with further information.

sorry for that.My native language is not English, so I may not have expressed myself clearly at the beginning

I think the output should be

08:31:35: Main : before creating thread
08:31:35: Main : before running thread
08:31:35: Thread 1: starting
08:31:35: Main : wait for the thread to finish
08:31:35: Main : all done

But actually the output is

08:31:35: Main : before creating thread
08:31:35: Main : before running thread
08:31:35: Thread 1: starting
08:31:35: Main : wait for the thread to finish
08:31:35: Main : all done
08:31:45: Thread 1: finishing

in ipynb file.

This would be one of rare occasions where screenshots would be helpful. With .ipynb file you mean an interactive notebook, right? If so, this is the expected behaviour. Just because a single cell finishes executing doesn’t mean that the python interpreter gets shut down, so background threads continue to run.

1 Like

Thank you,I think I understand the reason for this problem.