Why is the python thread count 2 at the beginning?

拈花ヽ惹草 提交于 2020-12-30 09:41:26

问题


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?


回答1:


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




回答2:


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



回答3:


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.



来源:https://stackoverflow.com/questions/40471711/why-is-the-python-thread-count-2-at-the-beginning

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!