[Python] How to exit python deamon thread gracefully

Discussão em 'Python' iniciado por Stack, Maio 18, 2025.

  1. Stack

    Stack Membro Participativo

    I have code like below

    def run():
    While True:
    doSomething()
    def main():
    thread = threading.thread(target = run)
    thread.setDaemon(True)
    thread.start()
    doSomethingElse()


    if I Write code like above, when the main thread exits, the Deamon thread will exit, but maybe still in the process of doSomething.

    The main function will be called outside, I am not allowed to use join in the main thread, is there any way I can do to make the Daemon thread exit gracefully upon the main thread completion.

    Continue reading...

Compartilhe esta Página