Reputation: 357
I have a main GUI to receive data from an amplifier and plot it. When I click a button, a new process is created and it is receiving data with a blocking method (that is why I need another process). My question is, how can I send the received data to the main GUI to plot it? Here is an scheme and what I've tried:
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_())
I think that it has to be possible, but I don't know how to adress the problem because I found that passing an object between processes actually copies the object but don't pass it in reference (so you can't call the methods on the original one) So, what do you think is the best option? Thank you very much!
Upvotes: 1
Views: 3025
Reputation: 10365
Python's multiprocessing
module offers several techniques for IPC. The easiest one for your situation is probably a Queue
: when manager_process
retrieves data it puts it on a shared Queue
instance. In the original process you run a separate thread that retrieves items from the queue and calls draw_data
.
Upvotes: 1