>多协程
>>什么是协程
协程即协作式程序,又可以称为‘微线程’或‘纤程’。所有的协程都是通过线程建的
>>yield实现多协程
# 使用yield实现协程
def producter(cons):
info =None #必须首先发送一个noen
cons.send(info)
for item in range(10):
if item %2 ==0:
info = 'title = chenxianq contert = 123456'
else:
info = 'title = chenxianq111 contert = 123456333'
print('生产者%s'%info)
cons.send(info)
def consmuser():
while True:
rev=yield
print('消费者%s'%rev)
def main():
con=consmuser()
producter(con)
if __name__ == '__main__':
main()
>>gevent
# 使用gevent 模式自动切换
import gevent
def producter():
global info
for item in range(10):
if item % 2 == 0:
info = 'title = chenxianq contert = 123456'
else:
info = 'title = chenxianq111 contert = 123456333'
print('生产者%s' % info)
gevent.sleep(1)
def consmuser():
while True:
rev = info
print('消费者%s' % rev)
gevent.sleep(1)
def main():
producter_gevent = gevent.spawn(producter)
consmuser_gevent = gevent.spawn(consmuser)
producter_gevent.join()
consmuser_gevent.join()
if __name__ == '__main__':
main()