1、统计多线程执行的时间-方式一
1 import threading,time 2 3 def insert_db(): 4 time.sleep(3) 5 print(‘insert_db over‘) 6 7 8 start_time = time.time() 9 for i in range(3): # 串行的方式 10 insert_db() 11 end_time = time.time() 12 print(‘串行的执行时间:‘,end_time-start_time) 13 14 start_time2 = time.time() 15 # 第二种方式,思路是判断当前存活的线程个数 16 for i in range(3): 17 t = threading.Thread(target = insert_db) 18 t.start() 19 20 # threading.activeCount()返回当前活跃的线程数 21 while threading.activeCount() != 1: 22 pass 23 end_time2 = time.time() 24 25 print(‘多线程的执行时间:‘,end_time2-start_time2) 26 print(‘over.....‘)
结果:
2、统计多线程执行时间的第二种方式
1 import threading,time 2 3 def insert_db(): 4 time.sleep(3) 5 print(‘insert_db over‘) 6 7 8 start_time = time.time() 9 for i in range(3): # 串行的方式 10 insert_db() 11 end_time = time.time() 12 print(‘串行的执行时间:‘,end_time-start_time) 13 14 # 统计多线程执行时间:方式一 15 threads = [] 16 start_time2 = time.time() 17 for i in range(3): 18 t = threading.Thread(target=insert_db) 19 t.start() 20 threads.append(t) 21 22 for t in threads: 23 t.join() # 等待子线程执行完成,主线程在接着执行 24 25 end_time2 = time.time() 26 27 print(‘多线程的执行时间:‘,end_time2-start_time2) 28 print(‘over.....‘)
3、统计多线程执行时间:错误方式
1 import threading,time 2 3 def insert_db(): 4 time.sleep(3) 5 print(‘insert_db over‘) 6 7 8 start_time = time.time() 9 for i in range(3): # 串行的方式 10 insert_db() 11 end_time = time.time() 12 print(‘串行的执行时间:‘,end_time-start_time) 13 14 # 统计多线程执行时间:方式一 15 threads = [] 16 start_time2 = time.time() 17 for i in range(3): 18 t = threading.Thread(target=insert_db) 19 t.start() 20 threads.append(t) 21 22 # for t in threads: 23 # t.join() # 等待子线程执行完成,主线程在接着执行 24 25 end_time2 = time.time() 26 27 # 多线程的执行时间: 0.0010039806365966797??为啥不对? 28 # 主线程,也就是程序一开始运行的时候,最初的那个线程 29 # 这里的时间是主线程跑完的时间,没包含子线程执行的时间 30 # 子线程,通过thread类实例化的线程,都是子线程 31 print(‘多线程的执行时间:‘,end_time2-start_time2) 32 print(‘over.....‘)
结果:
4、实例(下载图片)
1 import threading 2 import requests 3 import hashlib 4 5 def down_load(url): 6 name = hashlib.md5(url.encode()).hexdigest() 7 r = requests.get(url) 8 with open(‘%s.jpg‘%name,‘wb‘) as fw: 9 fw.write(r.content) 10 11 12 l = [ 13 ‘http://www.nnzhp.cn/wp-content/themes/QQ/images/logo.jpg‘, 14 ‘http://www.nnzhp.cn/wp-content/uploads/2016/12/2016aj5kn45fjk5-150x150.jpg‘, 15 ‘http://www.nnzhp.cn/wp-content/themes/QQ/images/thumbnail.png‘ 16 ] 17 18 # 串行下载 19 for url in l: 20 down_load(url) 21 22 # 多线程下载 23 for i in l: 24 t = threading.Thread(target=down_load,args=[i]) 25 t.start() 26 27 while threading.activeCount() != 1: 28 pass 29 30 print(‘down load over ...‘)
5、??
1 # #多线程运行函数是,函数的返回值是拿不到的 2 # #所以定义一个list,把函数运行的结果都存进去就ok 3 # case_result = [] 4 # 5 # def run_case(case_name): 6 # print(‘run case...‘,case_name) 7 # case_result.append({case_name:‘success‘})
6、守护线程
1 # 守护线程:一旦主线程死掉,那么守护线程不管有没有执行完成,全部结束 2 3 import threading 4 import time 5 6 def talk(name): 7 print(‘正在和%s聊天‘%name) 8 time.sleep(200) 9 10 def shipin(name): 11 print(‘正在和%s视频‘%name) 12 time.sleep(300) 13 14 print(‘qq主窗口‘) 15 t1 = threading.Thread(target=talk,args=[‘lzh‘]) 16 # 如果没有设置为守护线程,所有线程运行结束后,程序才会结束 17 # 设置线程为守护线程 18 t1.setDaemon(True) 19 t1.start() 20 21 22 t2 = threading.Thread(target=shipin,args=[‘zsb‘]) 23 # 设置线程为守护线程 24 t2.setDaemon(True) 25 t2.start() 26 27 28 time.sleep(5) 29 print(‘结束。。。‘) 30 31 #所有线程运行结束后,程序才会结束
运行结果:如下图,程序并没有结束
1 # 守护线程:一旦主线程死掉,那么守护线程不管有没有执行完成,全部结束 2 3 import threading 4 import time 5 6 def talk(name): 7 print(‘正在和%s聊天‘%name) 8 time.sleep(200) 9 10 def shipin(name): 11 print(‘正在和%s视频‘%name) 12 time.sleep(300) 13 14 print(‘qq主窗口‘) 15 t1 = threading.Thread(target=talk,args=[‘lzh‘]) 16 # 如果没有设置为守护线程,所有线程运行结束后,程序才会结束 17 # 设置线程为守护线程 18 t1.setDaemon(True) 19 t1.start() 20 21 22 t2 = threading.Thread(target=shipin,args=[‘zsb‘]) 23 # 设置线程为守护线程 24 t2.setDaemon(True) 25 t2.start() 26 27 28 time.sleep(5) 29 print(‘结束。。。‘)
运行结果:
7、线程锁??
1 # 线程锁 2 #多个线程同时操作同一个数据的时候,会有问题 3 4 import threading 5 6 count = 0 7 lock = threading.Lock() 8 9 def test(): 10 global count 11 12 #lock.acquire(timeout = 3) #加锁,单位?? 13 14 count += 1 15 16 # lock.release() #解锁 17 # 线程死锁 18 19 for i in range(100): 20 t = threading.Thread(target = test) 21 t.start() 22 23 print(count)
python-threading模块&多线程&守护线程&线程锁
原文:https://www.cnblogs.com/tour8/p/13126257.html