在 Python 的其他进程中从函数 运行 调用 class 方法

Call class method from function running in other process in Python

我有一个主 GUI 来接收来自放大器的数据并绘制它。当我单击一个按钮时,将创建一个新进程并使用阻塞方法接收数据(这就是我需要另一个进程的原因)。我的问题是,如何将接收到的数据发送到主 GUI 来绘制它?这是一个方案和我尝试过的方案:

import multiprocessing as mp

# Load the .ui file
gui_main_class = uic.loadUiType("gui_main.ui")[0]

# Function that receive the data (RUNS IN OTHER PROCESS)
def manager_process(gui_main):
    amp = AmpReceiver()
    while True
        data = amp.getData()
        **HERE IS WHERE I HAVE TO CALL draw_data(data) IN GuiMainClass TO PLOT THE DATA**


# Main class that instantiates the UI
class GuiMainClass(QtWidgets.QMainWindow, gui_main_class):

    def __init__(self, parent=None):
        QtGui.QWindow.__init__(self, parent)
        self.setupUi(self)
        self.pushButton.clicked.connect(self.receive_data_process)

    def receive_data_process(self):
        self.data_receiver_process = mp.Process(target=manager_process)
        self.data_receiver_process.start()

    def draw_data(self, data):
        CODE TO UPDATE THE PLOT (NOT INTERESTING FOR THE QUESTION)


# Main function
if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    application = GuiMainClass()
    sys.exit(app.exec_())

我认为这一定是可行的,但我不知道如何解决这个问题,因为我发现在进程之间传递对象实际上复制了对象但不在引用中传递它(所以你可以'调用原始方法)那么,您认为最好的选择是什么?非常感谢!

Python 的 multiprocessing 模块提供 several techniques for IPC. The easiest one for your situation is probably a Queue:当 manager_process 检索数据时,它会将其放在共享的 Queue 实例上。在原始进程中,您 运行 一个单独的线程从队列中检索项目并调用 draw_data.