编程语言
首页 > 编程语言> > python3 协程函数yeild

python3 协程函数yeild

作者:互联网

# -*- coding: utf-8 -*-
import time


def consumer(item):
    time.sleep(0.01)
    pass


def producer(target, seq):
    for item in seq:
        target(item)


start_time = time.time()
producer(consumer, range(1000))
print("正常执行时间:%s" % (time.time() - start_time))

# 正常执行时间:10.000572204589844


def consumer():
    time.sleep(0.01)
    while 1:
        item = yield


def producer(target, seq):
    for item in seq:
        target.send(item)


start_time = time.time()
g = consumer()  # 生成器函数
next(g)
producer(g, range(10000000))
print("协程函数执行时间:%s" % (time.time() - start_time))

# 协程函数执行时间:1.921109676361084

 

标签:协程,seq,producer,start,yeild,item,time,python3,target
来源: https://www.cnblogs.com/lilyxiaoyy/p/11037228.html