我正在尝试生成一个生成2个进程的程序,其中一个(sender
)每秒向另一个进程(receiver
)发送一个随机整数,并绘制一个法线(Gaussian) )使用值进行分配。
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()
但是它没有按我预期的那样工作。如果我运行它,它将显示一个pyqtgraph窗口,该窗口是空白的(但是我可以看到数据交换)。如果我添加QtGui.QApplication.instance().exec_()
,它将很好地显示该窗口,但现在数据交换不起作用。我怎么了如果能得到一些建议或帮助,我将不胜感激。
答案 0 :(得分:0)
如果有帮助,我发现:
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()