码迷,mamicode.com
首页 > 编程语言 > 详细

python-多线程例子

时间:2018-05-21 14:49:06      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:for   ram   __name__   style   star   join()   port   nbsp   end   

 1 from time import ctime
 2 import threading
 3 
 4 def coding(language):
 5     for i in range(5):
 6         print(I\‘m coding ,language,  program at , ctime() )
 7 
 8 def music():
 9     for i in range(5):
10         print(I\‘m listening music at , ctime())
11 
12 if __name__ == __main__:
13 
14     print(thread %s is running... % threading.current_thread().name)
15 
16     thread_list = []
17     t1 = threading.Thread(target=coding, args=(Python,))
18     t2 = threading.Thread(target=music)
19     thread_list.append(t1)
20     thread_list.append(t2)
21 
22     for t in thread_list:
23         t.setDaemon(True)  # 设置为守护线程
24         t.start()
25         t.join()  # 在这个子线程完成运行之前,主线程将一直被阻塞
26 
27     print(thread %s ended. % threading.current_thread().name)

 

python-多线程例子

标签:for   ram   __name__   style   star   join()   port   nbsp   end   

原文地址:https://www.cnblogs.com/LyShark/p/9066585.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!