23.协程的使用场景,高I/O密集型程序
import time def one_hundred_millionA(): start_time = time.time() i = 0 for _ in range(100000000): i = i +1 yield end_time = time.time() print("A--Total time:{}".format(end_time-start_time)) def one_hundred_millionB(a): start_time = time.time() i = 0 for _ in range(100000000): i = i +1 a.__next__() end_time = time.time() print("B--Total time:{}".format(end_time-start_time)) if __name__ == '__main__': a = one_hundred_millionA() one_hundred_millionB(a) #实现开发中协程往往用在高IO密集型的程序,对于高CPU密集型效果不明显