多线程Threading
多线程之间的资源是共享的,多线程之间是并发的
多线程的实现
import threading
import time
def music(name):
print('%s begin to listen to music %s'%(name,time.ctime()))
time.sleep(3)
print('%s end to listen to music %s'%(name,time.ctime()))
def game(name):
print('%s begin to play game %s'%(name,time.ctime()))
time.sleep(5)
print('%s end to play game %s'%(name,time.ctime()))
if __name__ == '__main__':
t1=threading.Thread(target=music,args=('alex',))#实例化多线程
t2=threading.Thread(target=game,args=('alex',))
t1.start()#开始执行多线程
t2.start()
print('end....') #注意时间只要5秒就完成
'''alex begin to listen to music Tue Apr 17 21:32:50 2018
alex begin to play game Tue Apr 17 21:32:50 2018
end....
alex end to listen to music Tue Apr 17 21:32:53 2018
alex end to play game Tue Apr 17 21:32:55 2018'''
join
import threading
import time
def music(name):
print('%s begin to listen to music %s'%(name,time.ctime()))
time.sleep(3)
print('%s end to listen to music %s'%(name,time.ctime()))
def game(name):
print('%s begin to play game %s'%(name,time.ctime()))
time.sleep(5)
print('%s end to play game %s'%(name,time.ctime()))
if __name__ == '__main__':
t1=threading.Thread(target=music,args=('alex',))#实例化多线程
t2=threading.Thread(target=game,args=('alex',))
t1.start()#开始执行多线程
t2.start()
t1.join() #join方法是让主线程等待子线程t1结束后才继续执行主线程
t2.join()
print('end....')
'''alex begin to listen to music Tue Apr 17 21:39:23 2018
alex begin to play game Tue Apr 17 21:39:23 2018
alex end to listen to music Tue Apr 17 21:39:26 2018
alex end to play game Tue Apr 17 21:39:28 2018
end....'''
若改成
if __name__ == '__main__':
t1=threading.Thread(target=music,args=('alex',))#实例化多线程
t2=threading.Thread(target=game,args=('alex',))
t1.start()#开始执行多线程
t2.start()
t1.join() #join方法是让主线程等待子线程t1结束后才继续执行主线程
#t2.join()
print('end....')
'''alex begin to listen to music Tue Apr 17 21:41:11 2018
alex begin to play game Tue Apr 17 21:41:11 2018
alex end to listen to music Tue Apr 17 21:41:14 2018
end....
alex end to play game Tue Apr 17 21:41:16 2018'''
#t1在执行完毕后会立即执行print,然后等待执行t2
Setdaemon
import threading
import time
def music(name):
print('%s begin to listen to music %s'%(name,time.ctime()))
time.sleep(3)
print('%s end to listen to music %s'%(name,time.ctime()))
def game(name):
print('%s begin to play game %s'%(name,time.ctime()))
time.sleep(5)
print('%s end to play game %s'%(name,time.ctime()))
if __name__ == '__main__':
t1=threading.Thread(target=music,args=('alex',))
t2=threading.Thread(target=game,args=('alex',))
t2.setDaemon(True) #必须在start前设置,一旦主线程结束,对应的子线程也结束
t1.start()
t2.start()
print('end....')
'''
alex begin to listen to music Thu Apr 19 15:22:42 2018
alex begin to play game Thu Apr 19 15:22:42 2018
end....
alex end to listen to music Thu Apr 19 15:22:45 2018
'''
#在打印end...后还要等待t1的运行,当t1运行完后,主线程才算结束,t2也跟着结束
若改成
if __name__ == '__main__':
t1=threading.Thread(target=music,args=('alex',))
t2=threading.Thread(target=game,args=('alex',))
t1.setDaemon(True) #必须在start前设置,一旦主线程结束,对应的子线程也结束
t1.start()
t2.start()
print('end....')
'''
alex begin to listen to music Thu Apr 19 15:26:46 2018
alex begin to play game Thu Apr 19 15:26:46 2018
end....
alex end to listen to music Thu Apr 19 15:26:49 2018
alex end to play game Thu Apr 19 15:26:51 2018
'''
#在打印end...之后要等待t2运行,但t1在t2运行结束之前已经运行,故当t2结束运行,主线程结束时t1也运行完毕
线程相关的其他方法
#Thread实例对象的方法
# isAlive(): 返回线程是否活动的。
# getName(): 返回线程名。
# setName(): 设置线程名。
#threading模块提供的一些方法:
# threading.currentThread(): 返回当前的线程变量。
# threading.enumerate(): 返回一个包含正在运行的线程的list。正在运行指线程启动后、结束前,不包括启动前和终止后的线程。
# threading.activeCount(): 返回正在运行的线程数量,与len(threading.enumerate())有相同的结果。
from threading import Thread
import threading
def work():
import time
time.sleep(3)
print(threading.current_thread().getName())
if __name__ == '__main__':
#在主进程下开启线程
t=Thread(target=work)
t.start()
print(threading.current_thread().getName())
print(threading.current_thread()) #主线程
print(threading.enumerate()) #连同主线程在内有两个运行的线程
print(threading.active_count())
print('主线程/主进程')
'''
打印结果:
MainThread
<_MainThread(MainThread, started 140735268892672)>
[<_MainThread(MainThread, started 140735268892672)>, <Thread(Thread-1, started 123145307557888)>]
主线程/主进程
Thread-1
'''
主线程等待子线程结束
from threading import Thread
import time
def sayhi(name):
time.sleep(2)
print('%s say hello' %name)
if __name__ == '__main__':
t=Thread(target=sayhi,args=('alex',))
t.start()
t.join()
print('主线程')
print(t.is_alive())
'''
alex say hello
主线程
False
'''