我的代码运行两分钟,然后出现此错误:
致命的Python错误:无法从堆栈溢出中恢复。 当前线程0x000001d4(最近调用优先):
我使用wxpython并从串行端口('COM9')中获取数据,而我的数据却是无限的。我使用wx.Timer
和list.append
。
为了避免堆栈溢出,我使用计时器删除了列表。在2分钟内,我运行了代码,一切都很好。但是,即使我删除了列表,并且列表的长度只有76个,我的应用程序仍然冻结并且无法继续。
也许我应该在wx中尝试wxCallAfter
和wxCallLater
或“线程和处理”,但在wx中不建议使用线程和处理)。
或者也许我不应该使用append.list
并稍后将其删除?如果问题是由于我的数据结构引起的,我应该怎么用呢?
# -*- coding: utf-8 -*-
import wx
from gui import *
#import _pickle as pickle
import pickle
import serial
import matplotlib.pyplot as plt
import matplotlib.pyplot
import numpy as np
import matplotlib.animation as animation
from test.test_zipfile import StoredBadCrcTests
import time
print("0")
ser = serial.Serial('COM9',9600)
fig, ax= plt.subplots()
x=[]
y=[]
x1=[]
x2=[]
y1=[]
y2=[]
i=0
storage=[]
class selam ( MyFrame1 ):
print("1")
def __init__( self, parent ):
MyFrame1.__init__ ( self, parent)
print("here")
self.m_timer1 = wx.Timer()
self.m_timer1.SetOwner( self, 1 )
self.m_timer2 = wx.Timer()
self.m_timer2.SetOwner( self, 2 )
self.m_timer3 = wx.Timer()
self.m_timer3.SetOwner( self, 3 )
self.m_timer4 = wx.Timer()
self.m_timer4.SetOwner( self, 4 )
# Connect Events
self.Bind( wx.EVT_TIMER, self.timer1, id=1 )
self.Bind( wx.EVT_TIMER, self.timer2, id=2 )
self.Bind( wx.EVT_TIMER, self.timer3, id=3 )
self.Bind( wx.EVT_TIMER, self.timer4, id=4 )
def __del__( self ):
pass
def tgl1( self, event ):
if self.m_timer1.IsRunning():
self.m_timer1.Stop()
print("timer 1 stopped")
else:
print("tgl_timer 1 starting...")
self.m_timer1.Start( 1000 )
def tgl2( self, event ):
if self.m_timer2.IsRunning():
self.m_timer2.Stop()
plt.close()
print("timer 2 stopped")
else:
print("tgl_timer 2 starting...")
self.m_timer2.Start( 501 )
def tgl3( self, event ):
if self.m_timer3.IsRunning():
self.m_timer3.Stop()
plt.close()
print("timer 3 stopped")
else:
print("tgl_timer 3 starting...")
self.m_timer3.Start( 2000 )
def tgl4( self, event ):
if self.m_timer4.IsRunning():
self.m_timer4.Stop()
plt.close()
print("timer 4 stopped")
else:
print("tgl_timer 4 starting...")
self.m_timer4.Start( 1000 )
########## data and all code has to written inside of timer(1,2,3,4..)
def timer1( self, event ):
print("1")
for line in ser:
storage.append(line)
#===================================================================
# f1 = open('C:\\Users\\GCS-User\\Desktop\\obj_pi','wb')
# pickle.dump(line,f1)
#
# f2=open('C:\\Users\\GCS-User\\Desktop\\obj_pi','rb')
# print(pickle.load(f2))
#===================================================================
def timer2( self, event ):
print("22")
del storage[:]
def timer3( self, event ):
print("333")
def timer4( self, event ):
print("4444")
def sV_sat(i):
#print("a")
for line in storage:
print("b")
print("storage_len:",len(storage))
data = line.split(b",")
#print("data_0:",data[0])
if data[0] == b'$GNGGA':
print("c")
altitude=data[9]
timm=data[1]
tim=float(timm)
tim=tim+30000
hour = tim//10000
minute = (tim//100)%100
second = tim%100
zaman = hour*3600 + minute*60 + second
altitude=float(altitude)
x.append(zaman)
y.append(altitude)
ax.plot(x,y)
ani8 = animation.FuncAnimation(fig,sV_sat)
plt.show()
if __name__ == "__main__":
app = wx.App(0)
frame = selam(None)
frame.Show()
app.MainLoop()