zoukankan      html  css  js  c++  java
  • Python--小功能应用

    #生产者消费者模型(单线程高并发)
    1、低效率模式  产完在统一给
    import time
    def producter():
        ret = []
        for i in range(10):
            time.sleep(0.1)
            ret.append(i)
        return  ret
    def consumer(res):
        for index,baozi in enumerate(res):
            time.sleep(0.1)
            print('第%s个人,吃了%s' %(index,baozi))
    res = producter()
    print(consumer(res))
    2、高效率模式  产一个给一个
    import time
    def consumer(name):
        print('我是[%s],我在排队买包子' %name)
        while True:
            baozi = yield
            time.sleep(1)   #模拟在achun在吃包子
            print('%s很开心,%s很好吃' %(name,baozi))
    def producter():
        population1 = consumer('achun')   #生成器函数
        population1.__next__()
        population2 = consumer('alin')  # 生成器函数
        population2.__next__()
        for i in range(10):
            time.sleep(1)   #模拟在在做包子
            population1.send('鲜肉菜包 %s' %i)
            population2.send('鲜肉菜包 %s' %i)
    producter()
     
  • 相关阅读:
    moment.js常用时间示例,时间管理
    RabbitMQ用户增删及权限控制
    CDN概念基本介绍
    在LinkedIn的 Kafka 生态系统
    发行说明
    Kafka 1.0版本发布
    redis应用场景及实例
    Redis哨兵集群
    redis-订阅与发布
    redis事务
  • 原文地址:https://www.cnblogs.com/Essaycode/p/10126515.html
Copyright © 2011-2022 走看看