主线程与子线程个数,以及分辨

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了主线程与子线程个数,以及分辨相关的知识,希望对你有一定的参考价值。

# 主线程与子线程
import threading
import time
def run(n):
    print(‘task‘,n,threading.current_thread())#threading.current_thread()打印当前线程
    time.sleep(2)
    print(‘task done‘,n)

for i in range(50):#循环里边全是子线程,t-1是子线程,不是主线程
    t= threading.Thread(target=run,args=(‘t-%s‘%i,))
    t.start()

#threading.current_thread()打印当前线程
print(‘主线程就是程序本身,这个程序一共有51个线程:50个子线程,1个主线程‘,threading.current_thread())#主线程

print(‘查看当前活动线程的个数:‘,threading.active_count())

  结果:

D:\Anaconda3\python.exe C:/Users/Administrator/Desktop/py_work/expreicise.py
task t-0 <Thread(Thread-1, started 6684)>
task t-1 <Thread(Thread-2, started 4680)>
task t-2 <Thread(Thread-3, started 7588)>
task t-3 <Thread(Thread-4, started 8024)>
task t-4 <Thread(Thread-5, started 3220)>
task t-5 <Thread(Thread-6, started 7800)>
task t-6 <Thread(Thread-7, started 6680)>
task t-7 <Thread(Thread-8, started 8560)>
task t-8 <Thread(Thread-9, started 8516)>
task t-9 <Thread(Thread-10, started 4356)>
task t-10 <Thread(Thread-11, started 4240)>
task t-11 <Thread(Thread-12, started 7048)>
task t-12 <Thread(Thread-13, started 7400)>
task t-13 <Thread(Thread-14, started 7676)>
task t-14 <Thread(Thread-15, started 3764)>
task t-15 <Thread(Thread-16, started 6468)>
task t-16 <Thread(Thread-17, started 5680)>
task t-17 <Thread(Thread-18, started 8036)>
task t-18 <Thread(Thread-19, started 1344)>
task t-19 <Thread(Thread-20, started 4688)>
task t-20 <Thread(Thread-21, started 8228)>
task t-21 <Thread(Thread-22, started 3032)>
task t-22 <Thread(Thread-23, started 7336)>
task t-23 <Thread(Thread-24, started 6424)>
task t-24 <Thread(Thread-25, started 6084)>
task t-25 <Thread(Thread-26, started 4144)>
task t-26 <Thread(Thread-27, started 4436)>
task t-27 <Thread(Thread-28, started 6652)>
task t-28 <Thread(Thread-29, started 8292)>
task t-29 <Thread(Thread-30, started 1436)>
task t-30 <Thread(Thread-31, started 5176)>
task t-31 <Thread(Thread-32, started 3700)>
task t-32 <Thread(Thread-33, started 6348)>
task t-33 <Thread(Thread-34, started 6308)>
task t-34 <Thread(Thread-35, started 8048)>
task t-35 <Thread(Thread-36, started 5644)>
task t-36 <Thread(Thread-37, started 908)>
task t-37 <Thread(Thread-38, started 2504)>
task t-38 <Thread(Thread-39, started 7428)>
task t-39 <Thread(Thread-40, started 8496)>
task t-40 <Thread(Thread-41, started 8676)>
task t-41 <Thread(Thread-42, started 7968)>
task t-42 <Thread(Thread-43, started 8628)>
task t-43 <Thread(Thread-44, started 3936)>
task t-44 <Thread(Thread-45, started 7596)>
task t-45 <Thread(Thread-46, started 7100)>
task t-46 <Thread(Thread-47, started 7084)>
task t-47 <Thread(Thread-48, started 6420)>
task t-48 <Thread(Thread-49, started 3612)>
task t-49 <Thread(Thread-50, started 6840)>
主线程就是程序本身,这个程序一共有51个线程:50个子线程,1个主线程 <_MainThread(MainThread, started 4312)>
查看当前活动线程的个数: 51
task done t-0
task done t-1
task done t-3
task done t-4
task done t-2
task done t-5
task done t-7
task done t-10
task done t-8
task done t-9
task done t-6
task done t-11
task done t-14
task done t-12
task done t-13
task done t-15
task done t-16
task done t-19
task done t-22
task done t-17
task done t-18
task done t-21
task done t-20
task done t-27
task done t-23
task done t-26
task done t-24
task done t-28
task done t-25
task done t-35
task done t-34
task done t-31
task done t-33
task done t-29
task done t-30
task done t-32
task done t-41
task done t-39
task done t-37
task done t-38
task done t-40
task done t-36
task done t-46
task done t-47
task done t-45
task done t-43
task done t-44
task done t-42
task done t-49
task done t-48

Process finished with exit code 0

  

以上是关于主线程与子线程个数,以及分辨的主要内容,如果未能解决你的问题,请参考以下文章

主线程与子线程之间相互通信(HandlerThread)

主线程与子线程的关系

多线程实现udp网络通信

如何与子线程共享变量?

从主线程发送信号给子线程,子线程里的connect函数怎么个写法

线程与子线程(python3入门)