简体   繁体   English

如何创建两个互相监视的对象?

[英]How can I create two objects that monitor each other?

My goal is to make two objects that interact, specifically, one object that creates data and appends it to a list, and another object that can check that list and output the data. 我的目标是使两个对象进行交互,具体地说,一个对象创建数据并将其附加到列表中,另一个对象可以检查该列表并输出数据。

Based on another stackoverflow question, someone recommended creating a third object to store the data, and having the first two objects get initiated with that. 根据另一个stackoverflow问题,有人建议创建第三个对象来存储数据,并用它来初始化前两个对象。

The code works something like this: 该代码的工作原理如下:

class Master:
    def __init__(self):
        self.data = []
    (some methods for interaction)

class A:
    def __init__(self, master_instance):
        ...
        self.master_instance = master_instance
        (call method that randomly generates data and adds it to the master_instance data
        , and run this for a while (essentially as long as I want this process to continue))
    def my_a_method(self):
        ...

class B:
    def __init__(self, master_instance):
        ...
        self.master_instance = master_instance
        (call method that monitors master_instance to see if object A has added any data 
        to it, and spit it out. Like above, run this for as long as I want the process 
        to continue)
    def my_b_method(self):
        ...

master = Master()
a = A(master)
b = B(master)

So ideally, both of the process run at the same time. 因此,理想情况下,两个过程都同时运行。 However, what ends up happening is the first object is created, sends out all the data, and then the second object runs, rather then running at the same time. 但是,最终发生的事情是创建了第一个对象,发送了所有数据,然后第二个对象运行,而不是同时运行。 It works in the sense that they can both share data, but it doesn't work in the sense of them both running at the same time. 从某种意义上说,它们可以共享数据,但在它们都同时运行的意义上,它是行不通的。

(This is an exercise in a book, from the chapter on classes, but they didn't really discuss how this would be done) (这是一本书的练习,摘自有关课程的一章,但他们并未真正讨论如何实现)

import multiprocessing as mp

queue = mp.Queue()

def adder(q):  # for the sake of this example, let's say we want to add squares of all numbers to the queue
    i = 0
    while i < 100:
        q.put(i**2)
        i += 1
    q.put(None)

def monitor(q):  # monitor the queue and do stuff with the data therein
    for e in iter(q.get, None):
        print(e)   # for now, let's just print the stuff


a = mp.Process(target=adder, args=(queue,))
b = mp.Process(target=monitor, args=(queue,))
a.start()
b.start()
a.join()  # wait for the process to finish
b.join()  # wait for the process to finish (both processes running simultaneously)
a.terminate()
b.terminate()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何声明 3 个相互作为属性的对象 - How can I declare 3 objects that have each other as an attribute 如何将检测到的对象相互比较 - How Can I Compare Detected Objects with Each Other 如何让 Python 中的对象相互通信? - How can I make Objects communicate with each other in Python? 如何在 Django 中检查两个模型是否相等? - How can I check if two models equal each other in Django? 如何在 python 中编写两个相邻的函数? - How can I write two functions next to each other in python? 如何使两个telnet实例相互交互? - How can I make two telnet instances interact with each other? 连接两个彼此对应的列表以创建一个类对象列表 - Joining two lists that correspond to each other to create a list of class objects 如何在Python中将两个子对象相互映射 - How to map two child objects to each other in Python 如何使两个带有 TKinter 的测试按钮运行两个相互独立的功能? Python3 - How can I make two test buttons with TKinter run two functions independent of each other? Python3 如何在不将它们相互合并的情况下检测此图像中具有白色背景的所有对象? - How can I detect all objects in this image with a white background without merging them with each other?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM