社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
def consumer():
while 1:
x=yield #函数暂停
print(x)
def producer():
g=consumer()
next(g)
for i in range(100):
g.send(i) #传值给yield
print ("is producing")
producer()
#结果输出
0
is producing
1
is producing
2
is producing
3
is producing
4
is producing
5
is producing
6
is producing
import gevent
def func1():
print ("1,2,3,4")
gevent.sleep(1)
print ("5,6,7,8")
gevent.sleep(1)
def func2():
print ("4,3,2,1")
gevent.sleep(1)
print ("8,7,6,5")
g1=gevent.spawn(func1)
g2=gevent.spawn(func2)
g1.join()
#结果
1,2,3,4
4,3,2,1
5,6,7,8
8,7,6,5
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!