在一個生成器函數未啟動之前,是不能傳遞數值進去。必須先傳遞一個None進去或者調用一次next(g)方法,才能進行傳值操作
def product(c):# c.send(None)for i in range(5):print("生產者產生數據%d"%i)r= c.send(str(i))print("消費者消費了數據%s"%r)# c.close()
def customer():data = ""while 1:n =yield dataif not n:returnprint("消費者消費了%s"%n)data= "200"
c= customer()
product(c)
?