''' Python的GIL: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython’s memory management is not thread-safe. (However, since the GIL exists, other features have grown to depend on the guarantees that it enforces.) 上面的核心意思就是,无论你启多少个线程,你有多少个cpu,Python在执行的时候会淡定的在同一时刻只允许一个线程运行 ''' import threading import time '''线程threading模块''' # def Hi(num): # print('NO:%s' % num) # time.sleep(3) # # if __name__ == '__main__': # t_1 = threading.Thread(target=Hi, args=(10,)) # 通过Thread类实例化创建一个线程对象t1,target参数传入需要执行的对象,args参数传入被执行对象的参数且必须为可迭代对象 # t_1.start() # 启动线程 # # t_2 = threading.Thread(target=Hi, args=(9,)) # t_2.start() # 启动线程 # # print('ending...') # 依旧是主线程 # 两个函数的打印结果同时显示,且同时打印了ending;运行顺序为主线程从上到下直到遇到t_1和t_2两个线程,主线程还有一个print,最后两个线程睡完后主线程就结束了运行 # def music(): # print('begin to listen %s' % time.ctime()) # time.sleep(3) # print('stop to listen %s' % time.ctime()) # # def playgame(): # print('begin to play game %s' % time.ctime()) # time.sleep(5) # print('stop to play game %s' % time.ctime()) # # if __name__ == '__main__': # t1 = threading.Thread(target=music) # t1.start() # # t2 = threading.Thread(target=playgame) # t2.start() # # print('主线程已结束... %s' % time.ctime()) # 现在只需要5秒左右就把程序运行完毕,而以前只有一个主线程需要8秒左右 '''join()方法:在子线程完成运行之前,这个子线程的父线程将一直被阻塞''' # 顺序情况 # def music_1(): # print('begin to listen music_1 %s' % time.ctime()) # time.sleep(3) # print('stop to listen music_1 %s' % time.ctime()) # # def playgame_1(): # print('begin to play game_1 %s' % time.ctime()) # time.sleep(5) # print('stop to play game_1 %s' % time.ctime()) # # if __name__ == '__main__': # t3 = threading.Thread(target=music_1) # t4 = threading.Thread(target=playgame_1) # # t3.start() # t4.start() # # t3.join() # t4.join() # # print('主线程已结束... %s' % time.ctime()) # 主线程先创建两个t3、t4线程,然后启动两个线程,主线程继续往下执行遇到t3.join,t3执行完后代表t3.join执行完毕运行下一步,主线程遇到t4.join ,又回到t4继续执行,t4执行完后再执行主线程的最后一个print # begin to listen music_1 Tue Jun 1 17:10:58 2021 # begin to play game_1 Tue Jun 1 17:10:58 2021 # stop to listen music_1 Tue Jun 1 17:11:01 2021 # stop to play game_1 Tue Jun 1 17:11:03 2021 # 主线程已结束... Tue Jun 1 17:11:03 2021 # 乱序情况1 # def music_1(): # print('begin to listen music_1 %s' % time.ctime()) # time.sleep(3) # print('stop to listen music_1 %s' % time.ctime()) # # def playgame_1(): # print('begin to play game_1 %s' % time.ctime()) # time.sleep(5) # print('stop to play game_1 %s' % time.ctime()) # # if __name__ == '__main__': # t3 = threading.Thread(target=music_1) # t4 = threading.Thread(target=playgame_1) # # t3.start() # t4.start() # # t3.join() # # print('主线程已结束... %s' % time.ctime()) # # t4.join() # 主线程先遇到t3.join,那么t3线程运行完毕后就直接运行主线程的print,此时等到t4运行完毕后执行最后一个t4.join,然后主线程也结束了运行 # begin to listen music_1 Tue Jun 1 17:10:01 2021 # begin to play game_1 Tue Jun 1 17:10:01 2021 # stop to listen music_1 Tue Jun 1 17:10:04 2021 # 主线程已结束... Tue Jun 1 17:10:04 2021 # stop to play game_1 Tue Jun 1 17:10:06 2021 # 乱序情况2 # def music_1(): # print('begin to listen music_1 %s' % time.ctime()) # time.sleep(3) # print('stop to listen music_1 %s' % time.ctime()) # # def playgame_1(): # print('begin to play game_1 %s' % time.ctime()) # time.sleep(5) # print('stop to play game_1 %s' % time.ctime()) # # if __name__ == '__main__': # t3 = threading.Thread(target=music_1) # t4 = threading.Thread(target=playgame_1) # # t3.start() # t4.start() # # t4.join() # # print('主线程已结束... %s' % time.ctime()) # # t3.join() # 主线程先遇到t4.join,那么t4线程运行完毕后就直接运行主线程的print,而t4线程运行完毕的时候t3也已经运行完毕,所以主线程的print执行完后直接遇到t3.join却发现t3早已运行完毕,所以主线程退出 # begin to listen music_1 Tue Jun 1 17:09:24 2021 # begin to play game_1 Tue Jun 1 17:09:24 2021 # stop to listen music_1 Tue Jun 1 17:09:27 2021 # stop to play game_1 Tue Jun 1 17:09:29 2021 # 主线程已结束... Tue Jun 1 17:09:29 2021 # 乱序情况3 # def music_1(): # print('begin to listen music_1 %s' % time.ctime()) # time.sleep(3) # print('stop to listen music_1 %s' % time.ctime()) # # def playgame_1(): # print('begin to play game_1 %s' % time.ctime()) # time.sleep(5) # print('stop to play game_1 %s' % time.ctime()) # # if __name__ == '__main__': # t3 = threading.Thread(target=music_1) # t4 = threading.Thread(target=playgame_1) # # t3.start() # t3.join() # # t4.start() # t4.join() # # print('主线程已结束... %s' % time.ctime()) # 创建t3线程后遇到t3.join,此时主线程等t3运行完毕后再创建t4,主线程然后遇到t4.join,又等到t4运行完毕后再运行主线程的print,从程序运行时间上去考虑和直接运行两个函数一样,无意义