为什么python thread count 2在开头?
Why is the python thread count 2 at the beginning?
import threading
print threading.activeCount()
输出:2
当此代码保存到文件中并且运行。
主线程怎么会是2呢?
当我们 运行 一个 foo.py 文件时,python 运行 默认情况下除了主线程之外还有另一个线程吗?
心理调试:你不是运行普通的Python解释器。普通的 Python 解释器不会启动额外的线程(除非你有一个奇怪的 PYTHONSTARTUP
文件),但其他解释器会。例如:
ipython
启动一个额外的线程以在后台保存命令历史记录(以避免延迟提示)
IDLE
被设计为使用多个进程通过套接字进行通信,它为您提供的交互式解释器使用守护线程执行后台套接字通信
尝试运行print threading.enumerate()
;它可能会告诉您后台线程在做什么(例如,ipython
正在使用名为 HistorySavingThread
的 Thread
子类,IDLE
s 是普通的 Thread
,但是它运行的函数被命名为 SockThread
,这让你知道它在做什么)。
默认的 运行 线程是 主线程 ,因此下一个线程将是主线程的子线程,因此它从计数 2[ 开始=18=].
示例:
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)>]
当我在 IDLE 中 运行 像你这样的程序时,它 returns 线程数为 2。
当我 运行 它在 Windows Shell 时,它 returns 线程数为 1。
很明显,IDLE 使用了一个额外的线程。
when 运行ning threading.enumerate() in IDLE,它说有两个线程运行ning:
MainThread 和 SockThread
当 运行在 Windows Shell 中使用相同的代码时,SockThread 不是 运行ning。
总而言之,当您 运行 python 在 IDLE 中编写代码时,SockThread 在后台 运行ning,这会为您的线程数增加 1。
import threading
print threading.activeCount()
输出:2
当此代码保存到文件中并且运行。
主线程怎么会是2呢?
当我们 运行 一个 foo.py 文件时,python 运行 默认情况下除了主线程之外还有另一个线程吗?
心理调试:你不是运行普通的Python解释器。普通的 Python 解释器不会启动额外的线程(除非你有一个奇怪的 PYTHONSTARTUP
文件),但其他解释器会。例如:
ipython
启动一个额外的线程以在后台保存命令历史记录(以避免延迟提示)IDLE
被设计为使用多个进程通过套接字进行通信,它为您提供的交互式解释器使用守护线程执行后台套接字通信
尝试运行print threading.enumerate()
;它可能会告诉您后台线程在做什么(例如,ipython
正在使用名为 HistorySavingThread
的 Thread
子类,IDLE
s 是普通的 Thread
,但是它运行的函数被命名为 SockThread
,这让你知道它在做什么)。
默认的 运行 线程是 主线程 ,因此下一个线程将是主线程的子线程,因此它从计数 2[ 开始=18=].
示例:
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)>]
当我在 IDLE 中 运行 像你这样的程序时,它 returns 线程数为 2。 当我 运行 它在 Windows Shell 时,它 returns 线程数为 1。 很明显,IDLE 使用了一个额外的线程。
when 运行ning threading.enumerate() in IDLE,它说有两个线程运行ning: MainThread 和 SockThread 当 运行在 Windows Shell 中使用相同的代码时,SockThread 不是 运行ning。
总而言之,当您 运行 python 在 IDLE 中编写代码时,SockThread 在后台 运行ning,这会为您的线程数增加 1。