10
import threading
print threading.activeCount()

output: 2

When this code is saved into a file and run.

How could it be 2 when it's the main thread?

Does python run another thread by default in addition to the main thread when we run a foo.py file?

CDspace
  • 2,639
  • 18
  • 30
  • 36
SadeepDarshana
  • 1,057
  • 18
  • 34
  • 1
    What are you testing this in? I'm guessing you're testing in `ipython` or `IDLE`, both of which use thread(s) for background work. The plain Python interpreter does not launch additional threads unless you've configured it to do so in `PYTHONSTARTUP` file or the like. – ShadowRanger Nov 07 '16 at 18:13
  • i get `1` (using python 3.5.2) – Olian04 Nov 07 '16 at 18:14

3 Answers3

14

Psychic debugging: You're not running in a plain Python interpreter. The plain Python interpreter doesn't launch extra threads (unless you have a weird PYTHONSTARTUP file), but other interpreters would. For example:

  • ipython launches an extra thread to save command history in the background (to avoid delaying the prompt)
  • IDLE is designed using multiple processes communicating over a socket, and the interactive interpreter it provides you is using a daemon thread to perform the background socket communication

Try running print threading.enumerate(); it might tell you what the background thread is doing (for example, ipython is using a Thread subclass named HistorySavingThread, IDLEs is plain Thread, but the function it runs is named SockThread which gives you a clue as to what it's doing).

ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
-1

The default running thread is Main thread, So the next thread will be child for the main thread hence it starts form count 2.

Example:

thread count is :    1
Enumerate thread count is :  [<_MainThread(MainThread, started 140735135682560)>]
Starting Thread-1
thread count is :    2
Enumerate thread count is :  [<_MainThread(MainThread, started 140735135682560)>, <myThread(Thread-1, started 123145306509312)>]
Starting Thread-2
thread count is :    3
Enumerate thread count is :  [<_MainThread(MainThread, started 140735135682560)>, <myThread(Thread-1, started 123145306509312)>, <myThread(Thread-2, started 123145310715904)>]
Exiting Main Thread
thread count is :    3
Enumerate thread count is :  [<_MainThread(MainThread, started 140735135682560)>, <myThread(Thread-1, started 123145306509312)>, <myThread(Thread-2, started 123145310715904)>]
Thread-1: Thu Jun 28 12:44:35 2018
Thread-1: Thu Jun 28 12:44:36 2018
Thread-2: Thu Jun 28 12:44:36 2018
Thread-1: Thu Jun 28 12:44:37 2018
Thread-1: Thu Jun 28 12:44:38 2018
Thread-2: Thu Jun 28 12:44:38 2018
Thread-1: Thu Jun 28 12:44:39 2018
Exiting Thread-1
Thread-2: Thu Jun 28 12:44:40 2018
Thread-2: Thu Jun 28 12:44:42 2018
Thread-2: Thu Jun 28 12:44:44 2018
Exiting Thread-2
Enumerate thread count is :  [<_MainThread(MainThread, started 140735135682560)>]
chiru
  • 789
  • 6
  • 5
-1

When I run a programm like yours in IDLE, it returns a thread count of 2. When I run it in Windows Shell, it returns a thread count of 1. So apparently, IDLE uses one extra thread.

when running threading.enumerate() in IDLE, it says there are two threads running: MainThread and SockThread when running the same code in Windows Shell, SockThread isn't running.

All in all, when you run python code in IDLE, SockThread is running in the background which adds 1 to your thread count.

  • Not to discourage you, but why are you posting a brand new answer four years late that duplicates (some of) the information from the existing, accepted answer? Find unanswered, or incompletely answered questions; duplicating existing answers needlessly helps no one. – ShadowRanger Jun 03 '20 at 18:38