python中使用gevent模块实现多协程处理
2023-03-26 本文已影响0人
逐梦余生
# coding:utf-8
import gevent
import time
info = None # 定义数据
def product_handle():
global info
for item in range(10):
if item % 2 == 0:
info = "title = 小李老师、content = 软件技术讲师"
else:
info = "title = 沐言优拓、content = www.yootk.com"
print("【生产者】%s" % info)
gevent.sleep(1)
def consumer_handle():
while True:
print("【消费者】%s" % info)
gevent.sleep(1)
def main():
product_gevent = gevent.spawn(product_handle)
consumer_gevent = gevent.spawn(consumer_handle)
product_gevent.join()
consumer_gevent.join()
if __name__ == '__main__':
main()