Reputation: 2046
I'm trying to make a program that spawns 2 processes, one of which(sender
) sends a random integer every second to the other process(receiver
) and it plots a normal (Gaussian) distribution using the value.
import multiprocessing as mp
import time
import random
import os
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
from pyqtgraph.dockarea import *
class PLT():
def __init__(self):
self.win2 = pg.GraphicsWindow(title="Basic plotting examples")
self.win2.resize(1000,600)
self.win2.setWindowTitle('pyqtgraph example: Plotting')
self.p2 = self.win2.addPlot(title="Updating plot")
self.curve = self.p2.plot(pen='y')
#QtGui.QApplication.instance().exec_() # ---> if it's in effect, the plotting window shows up, but the data exchange doesn't happen.
def update(self, data):
self.curve.setData(data)
class sender(mp.Process):
def __init__(self, pipe):
mp.Process.__init__(self)
self.pipe = pipe
def run(self):
print('SENDER PID: ', os.getpid() )
while True:
value = random.randint(0, 10)
self.pipe.send(value)
time.sleep(1)
class receiver(mp.Process):
def __init__(self, pipe):
mp.Process.__init__(self)
self.pipe = pipe
def run(self):
self.p = PLT()
print('RECEIVER PID: ', os.getpid() )
while True:
integer = self.pipe.recv()
print(integer)
self.p.update(np.random.normal(size=(10,1000))[integer%10])
if __name__ == '__main__':
mp.freeze_support()
print('MAIN PID: ', os.getpid() )
out_pipe, in_pipe = mp.Pipe()
p1 = sender(pipe=in_pipe)
p2 = receiver(pipe=out_pipe)
p1.start()
p2.start()
But it doesn't work as I expected. If I run it, it shows a pyqtgraph window which is blank(but I can see the data exchange). If I add QtGui.QApplication.instance().exec_()
, it shows the window well, but now the data exchange doesn't work. What did I wrong? I'd appreciate it if I could get some advice or help.
Upvotes: 1
Views: 1639
Reputation: 21
If that can help, I found that:
import multiprocessing as mp
import time
import random
import os
import numpy as np
import pyqtgraph as pg
from pyqtgraph.Qt import QtGui, QtCore
#from pyqtgraph.dockarea import *
class PLT():
def __init__(self):
self.win = pg.GraphicsWindow(title="Basic plotting examples")
self.win.setWindowTitle('pyqtgraph example: Plotting')
#self.win.resize(1000,600)
self.p2 = self.win.addPlot(title="Updating plot")
self.curve = self.p2.plot(pen='y')
#QtGui.QApplication.instance().exec_() # ---> if it's in effect, the plotting window shows up, but the data exchange doesn't happen.
def update(self, data):
self.curve.setData(data)
QtGui.QApplication.processEvents() # <--- Here is the way to update the window.
class sender(mp.Process):
def __init__(self, pipe):
mp.Process.__init__(self)
self.pipe = pipe
def run(self):
print('SENDER PID: ', os.getpid() )
while True:
value = random.randint(0, 10)
self.pipe.send(value)
time.sleep(.01)
class receiver(mp.Process):
def __init__(self, pipe):
mp.Process.__init__(self)
self.pipe = pipe
def run(self):
self.p = PLT()
print('RECEIVER PID: ', os.getpid() )
while True:
integer = self.pipe.recv()
print(integer)
self.p.update(np.random.normal(size=(10,1000))[integer%10])
if __name__ == '__main__':
mp.freeze_support()
print('MAIN PID: ', os.getpid() )
out_pipe, in_pipe = mp.Pipe()
p1 = sender(pipe=in_pipe)
p2 = receiver(pipe=out_pipe)
p1.start()
p2.start()
Upvotes: 2