首页 > 其他分享 >Producers and Consumers Model

Producers and Consumers Model

时间:2023-03-29 20:57:48浏览次数:28  
标签:Process join consumers Consumers food Producers process done Model

生产者消费者模型要点

主要使用 JoinableQueue, Process 类

JoinableQueue([maxsize]):这就像是一个Queue对象,但队列允许项目的使用者通知生成者项目已经被成功处理。
通知进程是使用共享的信号和条件变量来实现的。

参数介绍:

maxsize是队列中允许最大项数,省略则无大小限制。

方法介绍:

JoinableQueue的实例p除了与Queue对象相同的方法之外还具有:
q.task_done():使用者使用此方法发出信号,表示q.get()的返回项目已经被处理。
如果调用此方法的次数大于从队列中删除项目的数量,将引发ValueError异常
q.join():生产者调用此方法进行阻塞,直到队列中所有的项目均被处理。
阻塞将持续到队列中的每个项目均调用q.task_done()方法为止

-*- coding: utf-8 -*-

import os
import random
import time
from multiprocessing import JoinableQueue, Process


def producers(name, q):
    for i in range(10):
        time.sleep(random.randint(1, 3))
        food = '%s-%s' % (name, i)
        q.put(food)
        print(f"{os.getpid()} produce {food}")
    # what is the q.join() meaning? producer call the function to make sure all the food was token by consumers.
    q.join()


def consumers(q):
    while True:
        food = q.get()
        time.sleep(random.randint(1, 3))
        print(f"{os.getpid()} eat {food}")
        # what is the q.task_done() meaning? consumers eat all the food that it get from q(combine with the q.join).
        q.task_done()


if __name__ == '__main__':
    q = JoinableQueue()
    p1 = Process(target=producers, args=('hamburgers', q,))
    p2 = Process(target=producers, args=('apple', q,))
    p3 = Process(target=producers, args=('banana', q,))
    c1 = Process(target=consumers, args=(q,), daemon=True)
    c2 = Process(target=consumers, args=(q,), daemon=True)

    # consumer process will be shut down when the main process was done. because it is daemon subprocess
    # when the main process will be done ? when the 3 subprocess of producers finished ,all the food was made.
    # why the consumer will consume all the time?
    process_tuple = (p1, p2, p3, c1, c2)

    for i in process_tuple:
        i.start()

    for i in process_tuple[:3]:
        i.join()

    print('main process end')


标签:Process,join,consumers,Consumers,food,Producers,process,done,Model
From: https://www.cnblogs.com/mrsphere/p/17270273.html

相关文章

  • Magento getModel getSingleton等常用函数说明
    Mage::getModel在通常的PHP初始化类对象的时候,使用以下方式进行生成。$modelSales=newModelSales();但是在magento中初始化模型对象时候,使用以下的方式进行生成模......
  • (转)gorm系列-model
    原文:https://www.cnblogs.com/zisefeizhu/p/12788017.htmlGormModel在使用ORM工具时,通常我们需要在代码中定义模型(Models)与数据库中的数据表进行映射,在GORM中模型(Models......
  • tensorflow checkpoint转savedmodel
    checkpoint文件结构saved_model文件结构  importtensorflowastfdefconvert_model():trained_checkpoint_prefix='/home/tiwang/code/jupyter-notebo......
  • Learning model-based planning from scratch
    发表时间:2017文章要点:这篇文章想说,之前的文章去做planning的时候,都会去设计一个planning的方法。这篇文章提出了一个端到端的方法,Imagination-basedPlanner,不去设计plan......
  • juddiv3 tmodel的代码
    环境:juddiv3+tomcat6.0+MySQL5.1+MyEclipse7.5WSDL在UDDI中的注册:    我们有两种方法和UDDI进行通信:   一、用soapui工具直接编写SOAP消息和UDDI进行......
  • Difformer: Empowering Diffusion Models on the Embedding Space for Text Generatio
    目录概符号说明主要内容GaoZ.,GuoJ.,TanX.,ZhuY.,ZhangF.,BianJ.andXuL.Difformer:Empoweringdiffusionmodelsontheembeddingspacefortextgene......
  • spring-web中的annotation注解之十:@ModelAttribute
    spring-web中的annotation注解之十:@ModelAttribute1、@ModelAttribute注解在方法上:*该方法在Controller每个方法执行前会被调用,没有返回值的@ModelAttribute方法使用mo......
  • 【THM】OSI Model(OSI模型介绍)-学习
    本文相关的TryHackMe实验房间链接:https://tryhackme.com/room/osimodelzi本文相关内容:了解决定跨网络处理数据的各个阶段的基本网络框架。什么是OSI模型?OSI模型(开放系......
  • Django笔记八之model中Meta参数的使用
    前面介绍了model的字段属性,字段类型,这篇笔记介绍一下model的Meta选项。这个选项提供了一些参数,比如排序(ordering),表名(db_table)等。但这都不是必需的,都是作为可选项,......
  • [tensorflow]plot_model
    [ImportError:Failedtoimportpydot.Youmustinstallpydotandgraphvizforpydotprinttowork]python使用tensorflow库时报错importtensorflowtensorflow.ker......