|
假设我是一个程序猿,我想听歌,但是我又要打码,所以有:
我听完歌就去打码:
复制代码
- 1 #!/usr/bin/python3.4
- 2 # -*- coding: utf-8 -*-
- 3
- 4 import time
- 5
- 6 def matter1(music):
- 7 print("我想听这些歌")
- 8
- 9 for i in range(0,len(music)):
- 10 print("第" + str(i + 1) + "首歌是:" + str(music[i]))
- 11 # 当前时间为
- 12 print(time.strftime('%Y%H%M%S', time.localtime()))
- 13 # 假设每一首歌曲的时间是2秒
- 14 time.sleep(2)
- 15 print("切换下一首歌...")
- 16
- 17 def matter2(number):
- 18 print("我在打码")
- 19
- 20 j = 0
- 21 while j <= number:
- 22 print("我准备写入第" + str(j + 1) +"行代码")
- 23 j = j + 1
- 24 # 当前时间为
- 25 print(time.strftime('%Y%H%M%S', time.localtime()))
- 26 # 假设每写一行代码的时间为1秒
- 27 time.sleep(1)
- 28 print("写下一行代码...")
- 29
- 30 if __name__ == '__main__':
- 31
- 32 start = time.time()
- 33
- 34 # 设定我要听的歌为
- 35 music = ["music1","music2","music3"]
- 36 # 开始听歌
- 37 matter1(music)
- 38 # 设定我要打码的行数
- 39 number = 5
- 40 # 开始打码
- 41 matter2(number)
- 42
- 43 end = time.time()
- 44 print("完成的时间为:" + str(end - start))
复制代码
复制代码
记录来的完成时间为:
1
完成的时间为:12.007483959197998
时间上完全符合,但是身为一个程序猿,可以一边打码一边听歌,那么设计一个多线程,让他们同时进行:
复制代码
- 1 #!/usr/bin/python3.4
- 2 # -*- coding: utf-8 -*-
- 3
- 4 import time
- 5 import threading
- 6
- 7 def matter1(music):
- 8 print("我想听这些歌")
- 9
- 10 for i in range(0,len(music)):
- 11 print("第" + str(i + 1) + "首歌是:" + str(music[i]))
- 12 # 当前时间为
- 13 print(time.strftime('%Y%H%M%S', time.localtime()))
- 14 # 假设每一首歌曲的时间是2秒
- 15 time.sleep(2)
- 16 print("切换下一首歌...")
- 17
- 18 def matter2(number):
- 19 print("我在打码")
- 20
- 21 j = 0
- 22 while j <= number:
- 23 print("我准备写入第" + str(j + 1) +"行代码")
- 24 j = j + 1
- 25 # 当前时间为
- 26 print(time.strftime('%Y%H%M%S', time.localtime()))
- 27 # 假设每写一行代码的时间为1秒
- 28 time.sleep(1)
- 29 print("写下一行代码...")
- 30
- 31 if __name__ == '__main__':
- 32 # 设定我要听的歌为
- 33 music = ["music1","music2","music3"]
- 34
- 35 # 设定我要打码的行数
- 36 number = 5
- 37 # 建立一个新数组
- 38 threads = []
- 39 # 将听歌放入数组里面
- 40 thing1 = threading.Thread(target=matter1, args=(music,))
- 41 threads.append(thing1)
- 42 # 将打码放入数组里面
- 43 thing2 = threading.Thread(target=matter2, args=(number,))
- 44 threads.append(thing2)
- 45
- 46 # 开始时间
- 47 start = time.time()
- 48 # 写个for让两件事情都进行
- 49 for thing in threads:
- 50 # setDaemon为主线程启动了线程matter1和matter2
- 51 # 启动也就是相当于执行了这个for循环
- 52 thing.setDaemon(True)
- 53 thing.start()
- 54
- 55 # 结束时间
- 56 end = time.time()
- 57 print("完成的时间为:" + str(end - start))
复制代码
复制代码
但是直接就结束了?
1
完成的时间为:0.0010008811950683594
原来是setDaemon,主线程启动两个子线程后做事后,主线程就不管子线程是否运行完毕,直接往下运行,
直接运行到
1
print("完成的时间为:" + str(end - start))
然后程序就结束了,因此,为了防止子线程还没结束主线程就结束的意外情况,在程序里面加个join:
复制代码
- 1 import time
- 2 import threading
- 3
- 4 def matter1(music):
- 5 print("我想听这些歌")
- 6
- 7 for i in range(0,len(music)):
- 8 print("第" + str(i + 1) + "首歌是:" + str(music[i]))
- 9 # 当前时间为
- 10 print(time.strftime('%Y%H%M%S', time.localtime()))
- 11 # 假设每一首歌曲的时间是2秒
- 12 time.sleep(2)
- 13 print("切换下一首歌...")
- 14
- 15 def matter2(number):
- 16 print("我在打码")
- 17
- 18 j = 0
- 19 while j <= number:
- 20 print("我准备写入第" + str(j + 1) +"行代码")
- 21 j = j + 1
- 22 # 当前时间为
- 23 print(time.strftime('%Y%H%M%S', time.localtime()))
- 24 # 假设每写一行代码的时间为1秒
- 25 time.sleep(1)
- 26 print("写下一行代码...")
- 27
- 28 if __name__ == '__main__':
- 29 # 设定我要听的歌为
- 30 music = ["music1","music2","music3"]
- 31
- 32 # 设定我要打码的行数
- 33 number = 5
- 34 # 建立一个新数组
- 35 threads = []
- 36 # 将听歌放入数组里面
- 37 thing1 = threading.Thread(target=matter1, args=(music,))
- 38 threads.append(thing1)
- 39 # 将打码放入数组里面
- 40 thing2 = threading.Thread(target=matter2, args=(number,))
- 41 threads.append(thing2)
- 42
- 43 # 开始时间
- 44 start = time.time()
- 45 # 写个for让两件事情都进行
- 46 for thing in threads:
- 47 # setDaemon为主线程启动了线程matter1和matter2
- 48 # 启动也就是相当于执行了这个for循环
- 49 thing.setDaemon(True)
- 50 thing.start()
- 51
- 52 # 子线程没结束前主线程会被卡在这里
- 53 thing1.join()
- 54 thing2.join()
- 55 # 结束时间
- 56 end = time.time()
- 57 print("完成的时间为:" + str(end - start))
复制代码
复制代码
最后运行的时间就是打码的时间:
1
完成的时间为:6.003339052200317
这就真正做到了一边听歌一边打码的双手互博的状态,本文后面的那0.003333秒就别纠结了,系统运行程序
花个0.0033333秒不过分吧
偷懒打码打4行:
1
2
number = 4
完成的时间为:5.008083820343018
------------------------------我是快乐的分割线------------------------------
网上的多线程都是写成“类”的形式,这里写成函数不符合“大众”标准,那么就改成类的形式:
复制代码
- 1 #!/usr/bin/python3.4
- 2 # -*- coding: utf-8 -*-
- 3
- 4 import time
- 5 import threading
- 6
- 7 class MyThread(threading.Thread):
- 8 def __init__(self, func, args, name=''):
- 9 threading.Thread.__init__(self)
- 10 self.name = name
- 11 self.func = func
- 12 self.args = args
- 13 #self.counter = counter
- 14
- 15 def run(self):
- 16 # 某某线程要开始了
- 17 print(self.name + "开始了##################")
- 18
- 19 if self.name == "听歌线程":
- 20 matter1(music)
- 21 elif self.name == "打码线程":
- 22 matter2(number)
- 23 print(self.name + "结束了##################")
- 24
- 25 def matter1(music):
- 26 for i in range(0,len(music)):
- 27 print("第" + str(i + 1) + "首歌是:" + str(music[i]))
- 28 # 假设每一首歌曲的时间是2秒
- 29 time.sleep(2)
- 30 print("切换下一首歌...")
- 31
- 32 def matter2(number):
- 33 j = 0
- 34 while j <= number:
- 35 print("我准备写入第" + str(j + 1) +"行代码")
- 36 j = j + 1
- 37 # 假设每写一行代码的时间为1秒
- 38 time.sleep(1)
- 39 print("写下一行代码...")
- 40
- 41
- 42 if __name__ == '__main__':
- 43 # 设定我要听的歌为
- 44 music = ["music1","music2","music3"]
- 45
- 46 # 设定我要打码的行数
- 47 number = 4
- 48
- 49 # 开始时间
- 50 start = time.time()
- 51
- 52 thing1 = MyThread(matter1, music,"听歌线程")
- 53 thing2 = MyThread(matter2, number, "打码线程")
- 54 thing1.start()
- 55 thing2.start()
- 56 thing1.join()
- 57 thing2.join()
- 58
- 59 # 结束时间
- 60 end = time.time()
- 61 print("完成的时间为:" + str(end - start))
复制代码
|
|