What does sys.exit really do with multiple threads?

后端 未结 3 826
闹比i
闹比i 2020-11-28 16:06

I was really confused by sys.exit() in python. In python documentation, it says \"Exit from Python\"; does that mean when sys.exit() is called in a python progr

相关标签:
3条回答
  • 2020-11-28 16:14

    (Paraphrasing what's in the Python 2 documentation for Thread Objects)

    Normally a Python program exits only when there's nothing but daemon threads (ignoring itself) left running. The “main thread” object which corresponds to the initial thread of control in the program isn't a daemon thread. Threads created using threading.Thread inherit their daemonic status from the creating thread, so if that's the main thread, they will also be non-daemonic.

    This means that by default any threads created and started by your main program will prevent it from exiting if they are still running when the main thread is terminated (by sys.exit() or simply by just hitting the end of its code). In other words, the program exits only when no alive non-daemon threads (in other words, only daemon threads) are left.

    You can override this default behavior by explicitly setting✶✶ the daemon property of any created thread objects to True before starting it.

    if __name__=="__main__":
        t = threading.Thread(target=threadrun)
        t.daemon = True  # Explicitly set property.
        t.start()
        sys.exit()
    

    Which will allow the program to actually end when sys.exit() is called (although calling it explicitly like that isn't necessary since it's at the end of the script anyway).


    A daemon thread is one that runs in the background and does not prevent the interpreter from exiting. See Daemon Threads Explanation.

    ✶✶ In Python 3.3, a daemon keyword argument with a default value of None was added to the Thread class constructor which means that, starting from that version onwards, you can simply use:

        # Sets whether the thread is daemonic via "daemon" keyword argument.
        t = threading.Thread(target=threadrun, daemon=True)
    

    However, doing it separately via an explicit attribute assignment statement still works, and would therefore be the more version-portable way of doing it.

    0 讨论(0)
  • 2020-11-28 16:17

    As per the documentation sys.exit() raises SystemExit:

    Exit the interpreter by raising SystemExit(status).

    If SystemExit reaches the default exception handler, it calls handle_system_exit(), which more or less pushes through to Py_Finalize(), which in turn calls wait_for_thread_shutdown() in Python 2, so sys.exit() is the same as the normal falling off the bottom of the main module in waiting for all non-daemon threads to terminate.

    0 讨论(0)
  • 2020-11-28 16:31

    In your case, the end of the program is when the last thread will be terminated. Maybe kind of join() method(like in Java) in python will wait for other threads.

    Please, read this article(: there is a good explanation how to play with threads in your case Use of threading.Thread.join()

    and

    documentation https://docs.python.org/2/library/threading.html (but relax, it is only for additional knowledge.

    and read this article about daemon property(if you do not want to wait for others threads become terminated Meaning of daemon property on Python Threads

    0 讨论(0)
提交回复
热议问题