首页 > 编程语言 > 详细

多线程

时间:2020-04-12 00:13:54      阅读:63      评论:0      收藏:0      [点我收藏+]

多线程

 1 import threading
 2 import time
 3 
 4 
 5 def music():
 6     print(begin to listen music %s % time.ctime())
 7     time.sleep(3)
 8     print(stop to listen music %s % time.ctime())
 9 
10 
11 def game():
12     print(begin to play game%s % time.ctime())
13     time.sleep(5)
14     print(stop to play game%s % time.ctime())
15 
16 
17 if __name__ == __main__:
18     t1 = threading.Thread(target=music)
19     t1.start()
20     t2 = threading.Thread(target=game)
21     t2.start()
22     t2.join()  # 等待子线程结束
23     print(ending)
24 结果
25 begin to listen music Sat Apr 11 23:01:55 2020
26 begin to play gameSat Apr 11 23:01:55 2020
27 stop to listen music Sat Apr 11 23:01:58 2020
28 stop to play gameSat Apr 11 23:02:00 2020
29 ending
30 
31 进程完成,退出码 0

设置守护线程,可以让某个子线程跟随子线程结束而结束

 1 import threading
 2 import time
 3 
 4 
 5 def music():
 6     print(begin to listen music %s % time.ctime())
 7     time.sleep(3)
 8     print(stop to listen music %s % time.ctime())
 9 
10 
11 def game():
12     print(begin to play game%s % time.ctime())
13     time.sleep(5)
14     print(stop to play game%s % time.ctime())
15 
16 
17 l = list()
18 
19 t1 = threading.Thread(target=music)
20 
21 t2 = threading.Thread(target=game)
22 t2.setDaemon(True)
23 l.append(t1)
24 l.append(t2)
25 输出:
26 begin to listen music Sat Apr 11 23:17:56 2020
27 begin to play gameSat Apr 11 23:17:56 2020
28 ending
29 stop to listen music Sat Apr 11 23:17:59 2020
30 
31 进程完成,退出码 0

技术分享图片

 1 import threading
 2 import time
 3 
 4 
 5 def music():
 6     print(begin to listen music %s % time.ctime())
 7     time.sleep(3)
 8     print(stop to listen music %s % time.ctime())
 9 
10 
11 def game():
12     print(begin to play game%s % time.ctime())
13     time.sleep(5)
14     print(stop to play game%s % time.ctime())
15 
16 
17 l = list()
18 
19 t1 = threading.Thread(target=music)
20 
21 t2 = threading.Thread(target=game)
22 t2.setDaemon(True)
23 l.append(t1)
24 l.append(t2)
25 if __name__ == __main__:
26     for t in l:
27         # t.setDaemon(True)  # 守护线程,使该线程跟着主线程一起结束
28         t.start()
29         # print(threading.active_count())
30         print(t.getName())
31         print(threading.current_thread())
32     print(threading.active_count())
33     print(threading.enumerate())
34     print(threading.current_thread())
35     print(ending)
36 输出:
37 begin to listen music Sat Apr 11 23:32:12 2020Thread-1
38 <_MainThread(MainThread, started 9716)>
39 
40 begin to play gameSat Apr 11 23:32:12 2020
41 Thread-2
42 <_MainThread(MainThread, started 9716)>
43 3
44 [<_MainThread(MainThread, started 9716)>, <Thread(Thread-1, started 16748)>, <Thread(Thread-2, started daemon 14432)>]
45 <_MainThread(MainThread, started 9716)>
46 ending
47 stop to listen music Sat Apr 11 23:32:15 2020
48 
49 进程完成,退出码 0

 

多线程

原文:https://www.cnblogs.com/ch2020/p/12683126.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!