WELCOME

不积跬步,无以至千里;不积小流,无以成江海。

多任务-线程1

1.查看当前运行的线程

threading.enumerate()
 1 import threading
 2 import time
 3 
 4 
 5 def tes1():
 6     for i in range(5):
 7         print('****tes1****{}****'.format(i))
 8 
 9 
10 def tes2():
11     for i in range(5):
12         print('****tes2****{}****'.format(i))
13 
14 
15 def main():
16     t1 = threading.Thread(target=tes1)
17     t2 = threading.Thread(target=tes2)
18 
19     t1.start()
20     time.sleep(1)
21     print('1秒钟后tes1执行完成!')
22     t2.start()
23     time.sleep(1)
24     print('2秒钟后tes2执行完成!')
25     # 查看当前运行的线程
26     print(threading.enumerate())
27 
28 
29 if __name__ == '__main__':
30     main()

****tes1****0****
****tes1****1****
****tes1****2****
****tes1****3****
****tes1****4****
1秒钟后tes1执行完成!
****tes2****0****
****tes2****1****
****tes2****2****
****tes2****3****
****tes2****4****
2秒钟后tes2执行完成!
[<_MainThread(MainThread, started 10620)>]

Process finished with exit code 0

 

2.线程执行代码的封装

  通过使用threading模块能完成多任务的程序开发,为了让每个线程的封装性更完美,所以使用threading模块时,往往会定义一个新的子类class,

只要继承threading.Thread就可以了,然后重写run方法
 1 #coding=utf-8
 2 import threading
 3 import time
 4 
 5 class MyThread(threading.Thread):
 6     def run(self):
 7         for i in range(3):
 8             time.sleep(1)
 9             msg = "I'm "+self.name+' @ '+str(i) #name属性中保存的是当前线程的名字
10             print(msg)
11 
12 
13 if __name__ == '__main__':
14     t = MyThread()
15     t.start()

I`am Thread-1 @ 0
I`am Thread-1 @ 1
I`am Thread-1 @ 2

  python的threading.Thread类有一个run方法,用于定义线程的功能函数,可以在自己的线程类中覆盖该方法。而创建自己的线程实例后,通过Thread类的start方法,可以启动该线程,

交给python虚拟机进行调度,当该线程获得执行的机会时,就会调用run方法执行线程。

3.多线程-共享全局变量

 1 import threading
 2 import time
 3 
 4 g_num = 100
 5 print('创建线程之前的g_num={}'.format(g_num))
 6 
 7 
 8 def tes1():
 9     global g_num
10     g_num += 1
11     print('****in tes1 g_num={}***'.format(g_num))
12 
13 
14 def tes2():
15     print('****in tes2 g_num={}***'.format(g_num))
16 
17 
18 def main():
19     t1 = threading.Thread(target=tes1)
20     t2 = threading.Thread(target=tes2)
21     t1.start()
22     time.sleep(1)
23     t2.start()
24     time.sleep(1)
25 
26     print('****in main Thread g_num={}***'.format(g_num))
27 
28 
29 if __name__ == '__main__':
30     main()

创建线程之前的g_num=100
****in tes1 g_num=101***
****in tes2 g_num=101***
****in main Thread g_num=101***

 

4.列表当作实参传递到线程中

 1 import threading
 2 import time
 3 
 4 g_nums = [11, 22]
 5 print('创建线程之前的g_nums={}'.format(g_nums))
 6 
 7 
 8 def tes1(temp):
 9     temp.append(33)
10     print('****in tes1 g_nums={}***'.format(str(temp)))
11 
12 
13 def tes2(temp):
14     print('****in tes2 g_nums={}***'.format(str(temp)))
15 
16 
17 def main():
18     # target指定这个线程去哪个函数执行代码
19     # args指定将来调用函数的时候传递什么数据过去
20     t1 = threading.Thread(target=tes1, args=(g_nums,))
21     t2 = threading.Thread(target=tes2, args=(g_nums,))
22     t1.start()
23     time.sleep(1)
24     t2.start()
25     time.sleep(1)
26 
27     print('****in main Thread g_nums={}***'.format(str(g_nums)))
28 
29 
30 if __name__ == '__main__':
31     main()

创建线程之前的g_nums=[11, 22]
****in tes1 g_nums=[11, 22, 33]***
****in tes2 g_nums=[11, 22, 33]***
****in main Thread g_nums=[11, 22, 33]***

 

posted @ 2022-04-18 17:35  Ambitious~  阅读(31)  评论(0)    收藏  举报