Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7010421
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:59:18+00:00 2026-05-27T21:59:18+00:00

I have several questions regarding Python threads. Is a Python thread a Python or

  • 0

I have several questions regarding Python threads.

  1. Is a Python thread a Python or OS implementation?
  2. When I use htop a multi-threaded script has multiple entries – the same memory consumption, the same command but a different PID. Does this mean that a [Python] thread is actually a special kind of process? (I know there is a setting in htop to show these threads as one process – Hide userland threads)
  3. Documentation says:

A thread can be flagged as a “daemon thread”. The significance of this
flag is that the entire Python program exits when only daemon threads
are left.

My interpretation/understanding was: main thread terminates when all non-daemon threads are terminated.

So python daemon threads are not part of Python program if “the entire Python program exits when only daemon threads are left”?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T21:59:19+00:00Added an answer on May 27, 2026 at 9:59 pm
    1. Python threads are implemented using OS threads in all implementations I know (C Python, PyPy and Jython). For each Python thread, there is an underlying OS thread.

    2. Some operating systems (Linux being one of them) show all different threads launched by the same executable in the list of all running processes. This is an implementation detail of the OS, not of Python. On some other operating systems, you may not see those threads when listing all the processes.

    3. The process will terminate when the last non-daemon thread finishes. At that point, all the daemon threads will be terminated. So, those threads are part of your process, but are not preventing it from terminating (while a regular thread will prevent it). That is implemented in pure Python. A process terminates when the system _exit function is called (it will kill all threads), and when the main thread terminates (or sys.exit is called), the Python interpreter checks if there is another non-daemon thread running. If there is none, then it calls _exit, otherwise it waits for the non-daemon threads to finish.


    The daemon thread flag is implemented in pure Python by the threading module. When the module is loaded, a Thread object is created to represent the main thread, and it’s _exitfunc method is registered as an atexit hook.

    The code of this function is:

    class _MainThread(Thread):
    
        def _exitfunc(self):
            self._Thread__stop()
            t = _pickSomeNonDaemonThread()
            if t:
                if __debug__:
                    self._note("%s: waiting for other threads", self)
            while t:
                t.join()
                t = _pickSomeNonDaemonThread()
            if __debug__:
                self._note("%s: exiting", self)
            self._Thread__delete()
    

    This function will be called by the Python interpreter when sys.exit is called, or when the main thread terminates. When the function returns, the interpreter will call the system _exit function. And the function will terminate, when there are only daemon threads running (if any).

    When the _exit function is called, the OS will terminate all of the process threads, and then terminate the process. The Python runtime will not call the _exit function until all the non-daemon thread are done.

    All threads are part of the process.


    My interpretation/understanding was: main thread terminates when all
    non-daemon threads are terminated.

    So python daemon threads are not part of python program if “the entire
    Python program exits when only daemon threads are left”?

    Your understanding is incorrect. For the OS, a process is composed of many threads, all of which are equal (there is nothing special about the main thread for the OS, except that the C runtime add a call to _exit at the end of the main function). And the OS doesn’t know about daemon threads. This is purely a Python concept.

    The Python interpreter uses native thread to implement Python thread, but has to remember the list of threads created. And using its atexit hook, it ensures that the _exit function returns to the OS only when the last non-daemon thread terminates. When using “the entire Python program”, the documentation refers to the whole process.


    The following program can help understand the difference between daemon thread and regular thread:

    import sys
    import time
    import threading
    
    class WorkerThread(threading.Thread):
    
        def run(self):
            while True:
                print 'Working hard'
                time.sleep(0.5)
    
    def main(args):
        use_daemon = False
        for arg in args:
            if arg == '--use_daemon':
                use_daemon = True
        worker = WorkerThread()
        worker.setDaemon(use_daemon)
        worker.start()
        time.sleep(1)
        sys.exit(0)
    
    if __name__ == '__main__':
        main(sys.argv[1:])
    

    If you execute this program with the ‘–use_daemon’, you will see that the program will only print a small number of Working hard lines. Without this flag, the program will not terminate even when the main thread finishes, and the program will print Working hard lines until it is killed.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Qt Creator and I have several questions regarding multiple build configurations.
I have several questions regarding filenames and the iPod Library. I understand I can
Need to use own imaged markers instead built-in pins. I have several questions. 1.
There have been several questions over the past few days about the proper use
I have several questions regarding forms and PHP but if I should put them
I have several questions regarding where to handle nulls. Let me set up a
(Note: I have seen several questions regarding .NET logging frameworks, but haven't seen any
I have been through several SO questions regarding this and my approach is a
I have several questions regarding the Google Map V3 API and its library for
I have several questions regarding CI application design. Q. When creating a new form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.