python threading(多线程)

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())

猜你喜欢

转载自www.cnblogs.com/zhaowei5/p/9300353.html