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

python threading(多线程)

时间:2018-07-12 18:01:12      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:listen   python   app   end   守护   int   完成   cti   守护线程   

join():在子线程完成运行之前,这个子线程的父线程将一直被阻塞。

setDaemon(True):守护线程。主线程结束不会等待这个子线程,直接结束。

 

import threading
from time import ctime,sleep
import time

def music(func):
for i in range(2):
print ("Begin listening to %s. %s" %(func,ctime()))
sleep(2)
print("end listening %s"%ctime())

def move(func):
for i in range(2):
print ("Begin watching at the %s! %s" %(func,ctime()))
sleep(3)
print(‘end watching %s‘%ctime())

threads = []
t1 = threading.Thread(target=music,args=(‘安静‘,))
threads.append(t1)
t2 = threading.Thread(target=move,args=(‘阿甘‘,))
threads.append(t2)

if __name__ == ‘__main__‘:

for t in threads:
# t.setDaemon(True)
t.start()
# t.join()
t1.join()
# t2.join()
print ("all over %s" %ctime())

python threading(多线程)

标签:listen   python   app   end   守护   int   完成   cti   守护线程   

原文地址:https://www.cnblogs.com/zhaowei5/p/9300353.html

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