Number of active threads

Python version: '2.7.3 (default, April 10, 2013 06:20:15) \ n [GCC 4.6.3]'

I have it:

#!/usr/bin/env python
import time, threading, os
def f1(arg1):
    for i in xrange(arg1):
        time.sleep(1)
        print "i is: ", i
        print threading.active_count()
        print threading.enumerate()

if __name__ == '__main__':

    t = threading.Thread(name="MyThread1", target=f1, args=(5,))
    t.start()

My question is why the number of active threads is reported as 2 and why the list generated enumeratealso contains the main thread.

I thought the main thread ends after the appearance of "MyThread1".

$. / threadeg.py

i is:  0
2
[<_MainThread(MainThread, stopped 139858183157504)>, <Thread(MyThread1, started 139858153768704)>]

i is:  1
2
[<_MainThread(MainThread, stopped 139858183157504)>, <Thread(MyThread1, started 139858153768704)>]

i is:  2
2
[<_MainThread(MainThread, stopped 139858183157504)>, <Thread(MyThread1, started 139858153768704)>]

i is:  3
2
[<_MainThread(MainThread, stopped 139858183157504)>, <Thread(MyThread1, started 139858153768704)>]

i is:  4
2
[<_MainThread(MainThread, stopped 139858183157504)>, <Thread(MyThread1, started 139858153768704)>]
+3
source share
1 answer

threading.activeCount (), which returns the number of active threads (which were started from this module). [Source] C>

Since the streaming module is a pure python module built on top of the stream module, it is fairly easy to view the source code.

Here is the source code for active_count

def activeCount():
    """Return the number of Thread objects currently alive.

    The returned count is equal to the length of the list returned by
    enumerate().

    """
    with _active_limbo_lock:
        return len(_active) + len(_limbo)

, _MainThread _active (_active _limbo - , ). _active _exitfunc.

_MainThread,

class _MainThread(Thread):

    def __init__(self):
        Thread.__init__(self, name="MainThread")
        self._Thread__started.set()
        self._set_ident()
        with _active_limbo_lock:
            _active[_get_ident()] = self

    def _set_daemon(self):
        return False

    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()

_exitfunc _MainThread , - , Thread._delete, mangled to __Thread_delete, , , _MainThread _active.

_exitfunc _shutdown 1201.

_shutdown = _MainThread()._exitfunc

_shutdown pythonrun.c, , , Py_Finalize. Py_Finalize Py_Exit, ( ).

Py_Exit.

. Py_Finalize(), C ().

, .

import threading, time

def f():
    time.sleep(1) #wait for the interpreter to "shutdown"
    print threading.enumerate()

if __name__ == '__main__':

    t = threading.Thread(target=f)
    t.daemon = True
    t.start()

    threading._shutdown() #simulate an interpreter shutdown

, , .

+1

All Articles