如何使用pyqtgraph和多处理进行绘图?

问题描述 投票:0回答:1

我正在尝试制作一个产生2个进程的程序,其中一个(sender)每秒向另一个进程(receiver)发送一个随机整数,并使用该值绘制正态(高斯)分布。

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_(),它会很好地显示窗口,但现在数据交换不起作用。我错了什么?如果我能得到一些建议或帮助,我会很感激。

python python-multiprocessing pyqtgraph
1个回答
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()
© www.soinside.com 2019 - 2024. All rights reserved.