2013-11-26 32 views
1

我試圖繪製服務器接收的實時數據,同時這樣做我的CPU(Windows XP操作系統)的RAM memroy增加,我採取了最新的更新Luke-campagnola,從鏈接「http://bazaar.launchpad.net/~luke-campagnola/pyqtgraph/inp/files/312/tools/debian/RAM內存增加,同時使用pyQtGraph生成兩個Axix繪圖

任何幫助或建議提前讚賞。

我附上這裏的整個代碼...

import sys 
from pyqtgraph.Qt import QtGui, QtCore 
import pyqtgraph as pg 
import collections 
import random 
import time 
import math 
import numpy as np 
from pyqtgraph.ptime import time 


class DynamicPlotter(QtGui.QWidget): 
    def __init__(self, parent = None):  
     QtGui.QWidget.__init__(self, parent) 

     sampleinterval=0.1 
     self.myvar = 0 
     self.prevdt=0 
     self.dt1 = 0 


     app = QtGui.QApplication([]) 

     self.plot_param1 = [] 
     self.plot_param2 = [] 
     self.plot_param3 = [] 
     self.plot_param4 = [] 
     for g in range(0,100): 
      self.plot_param1.append(g/100) 
      self.plot_param2 .append((g+1)/100) 
      self.plot_param3.append(g/100) 
      self.plot_param4.append((g+1)/100) 

     self.samplesize = 100; 
     self.samples  = range(0,self.samplesize) 

     for i in range(len(self.samples)): 
      self.samples[i] = self.samples[i]/100.0 

     self.framecount = 0; 

     pg.mkQApp() 
     self.pw = pg.PlotWidget() 

     self.pw.show() 
     self.p1 = self.pw.plotItem 
     self.p2 = pg.ViewBox() 
     self.p1.showAxis('right') 
     self.p1.scene().addItem(self.p2) 
     self.p2.setGeometry(self.p1.vb.sceneBoundingRect()) 
     self.p1.getAxis('right').linkToView(self.p2) 
     self.p2.setXLink(self.p1) 

     self.pw.setLabel('bottom', 'Time in Secs') 
     self.pw.setLabel('left', 'Velocity in rpm') 
     self.pw.setLabel('right', 'load in Nm') 

    def update(self): 

     self.p1.plot(self.samples, self.plot_param3) 

     self.p2.addItem(self.p1.plot(self.samples, self.plot_param4, pen='b')) 


     self.dt1 = self.dt1+1 

     self.p1.setYRange(min(self.plot_param3), max(self.plot_param3)) 
     self.p2.setXRange(self.dt1-1,self.dt1) 
     self.p2.setYRange(min(self.plot_param4), max(self.plot_param4)) 

     if self.framecount == 0:  
      flushloop = self.samplesize 
     else: 
      flushloop = self.samplesize+1 

     for flush in range(1,flushloop):  

      self.samples.pop(0) 


    # below code is to prepare for next sample 
    self.framecount = self.framecount + 1 
    k=0 
    for update in range(self.framecount*self.samplesize, 
     self.framecount*self.samplesize+self.samplesize): 
     if(0): 
      self.plot_param1.append(self.framecount+(update/2)) 
      self.plot_param2.append(self.framecount+ (update/2)) 
      self.myvar=self.myvar-1 
     else: 
      self.myvar=self.myvar+1 
      #self.plot_param2.append(self.framecount+ (update/2)) 
      #self.plot_param1.append(self.framecount+(update/2)) 


     self.samples.append(update/100.0) 

if(self.dt1<100): 
    self.plot_param1 = np.cos(np.linspace(0, 2*np.pi, 101)) 
    self.plot_param3 = self.plot_param1.tolist() 
    self.plot_param2 = np.sin(np.linspace(0, 4*np.pi, 101)) 
    self.plot_param4 = self.plot_param2.tolist() 

if((self.dt1>=100)and (self.dt1 < 200)): 
    self.plot_param1 = np.cos(np.linspace(0, 20*np.pi, 101)) 
    self.plot_param3 = self.plot_param1.tolist() 
    self.plot_param2 = np.sin(np.linspace(0, 40*np.pi, 101)) 
    self.plot_param4 = self.plot_param2.tolist() 

if((self.dt1>=200)and (self.dt1 < 300)): 
    for f in range(0,100): 

     self.plot_param1 = np.cos(np.linspace(0, 20*np.pi, 101)) 
    self.plot_param3 = self.plot_param1.tolist() 
    #self.plot_param3.append(1+f) 
    self.plot_param2 = np.cos(np.linspace(0, 20*np.pi, 101)) 
    #self.plot_param4.append(3+f) 
    self.plot_param4 = self.plot_param2.tolist() 




if(self.dt1 >= 300): 
    self.plot_param1 = np.cos(np.linspace(0, 10*np.pi, 101)) 
    self.plot_param3 = self.plot_param1.tolist() 
    self.plot_param2 = np.sin(np.linspace(0, 80*np.pi, 101)) 
    self.plot_param4 = self.plot_param2.tolist() 



    for i in range (len(self.plot_param3)): 

     self.plot_param3[i] = 20 * self.plot_param3[i] 



if __name__ == "__main__": 
    app = QtGui.QApplication(sys.argv)  
    myapp = DynamicPlotter() 
    for i in range(0,100): 
     myapp.update() 


    timer = QtCore.QTimer()   
    timer.timeout.connect(myapp.update) 
    timer.start(50) 

回答

1

有與您發佈的代碼的幾個問題:

  1. update()增加了新的情節曲線每次情景嗎被調用,這可能是您的內存使用的原因。它也會影響性能。爲了解決這個問題,你可以在update()

  2. 開頭的代碼試圖創建QApplication的情況下,至少三次,這可能會導致崩潰或掛起致電self.p1.clear()

    • DynamicPlotter.__init__有一行app = QtGui.QApplication([])
    • DynamicPlotter.__init__有另一行pg.mkQApp(),它不位於底部的相同
    • __main__塊還要求QtGui.QApplication([])

    另請注意,對於DynamicPlotter的每個實例,QApplication()將被調用一次。函數pg.mkQApp()可以安全地調用多次,因爲它會檢查QAp​​plication是否已經創建。

  3. DynamicPlotter是QWidget的子類,但既不顯示也不用作小部件。這是一個小問題,但稍後可能會引起混淆。

0

我更新了代碼,根據您的意見,我能夠解決內存問題。

當我去調試內存問題,我還觀察到

1) File PlotItem.py function addItem() has 
    self.items.append(item) 
    self.dataItems.append(item) 
    self.curves.append(item) 

2) File ViewBox.py function addItem() has 
    self.addedItems.append(item) 

上面提到的4列表項不斷積累,他們不釋放之前存儲的實例。

不太清楚爲什麼需要積累,我使用下面列出的命令刪除陣列。

del self.items [0:len(self.items)] 
del self.dataItems [0:len(self.dataItems)] 
del self.curves [0:len(self.curves)] 
del self.addedItems [0:len(self.addedItems)] 

沒通過將上述4行命令的發現任何問題。你能否提供相關信息?

Regards, Pradeep K.