2017-02-27 48 views
-1

我目前正在开发一个基于python的Raspberry pi 2应用程序,它有1GB内存。如何通过python线程减少内存消耗

在我的python应用程序中,我使用线程概念来显示进度条(线程将执行主进程,正常循环执行用户界面中的进度条)。

对于每次执行线程时,RAM存储器减少8mb,并且在某些时间程序挂起后。

有时线程上的命令os.system("raspistill") is not working否则它说maximum recursion depth exceeded

任何人都可以帮我清除上述情况吗?

def waiting2(wt): 
    wt.minsize(480,320) 
    wt.title("Reading") 
    wt.attributes('-fullscreen',True) 
    def animate(): 
     """ cycle through """ 
     img = next(pictures) 
     label["image"] = img 
     wt.after(delay, animate) 
    label = Tkinter.Label(wt) 
    label.place(x=150,y=60) 
    fname_list = \ 
    ['/home/pi/1/1 (1).gif', 
    '/home/pi/1/1 (2).gif', 
    '/home/pi/1/1 (3).gif', 
    '/home/pi/1/1 (4).gif', 
    '/home/pi/1/1 (5).gif', 
    '/home/pi/1/1 (6).gif', 
    '/home/pi/1/1 (7).gif', 
    '/home/pi/1/1 (8).gif', 
    '/home/pi/1/1 (9).gif', 
    '/home/pi/1/1 (10).gif', 
    '/home/pi/1/1 (11).gif', 
    '/home/pi/1/1 (12).gif', 
    '/home/pi/1/1 (13).gif', 
    '/home/pi/1/1 (14).gif', 
    '/home/pi/1/1 (15).gif', 
    '/home/pi/1/1 (16).gif', 
    '/home/pi/1/1 (17).gif', 
    '/home/pi/1/1 (18).gif', 
    '/home/pi/1/1 (19).gif', 
    '/home/pi/1/1 (20).gif', 
    '/home/pi/1/1 (21).gif', 
    '/home/pi/1/1 (22).gif', 
    '/home/pi/1/1 (23).gif', 
    '/home/pi/1/1 (24).gif', 
    '/home/pi/1/1 (25).gif', 
    '/home/pi/1/1 (26).gif', 
    '/home/pi/1/1 (27).gif', 
    '/home/pi/1/1 (28).gif', 
    '/home/pi/1/1 (29).gif', 
    '/home/pi/1/1 (30).gif', 
    '/home/pi/1/1 (31).gif', 
    '/home/pi/1/1 (32).gif', 
    '/home/pi/1/1 (33).gif', 
    '/home/pi/1/1 (34).gif', 
    '/home/pi/1/1 (35).gif', 
    '/home/pi/1/1 (36).gif', 
    '/home/pi/1/1 (37).gif', 
    '/home/pi/1/1 (38).gif', 
    '/home/pi/1/1 (39).gif', 
    '/home/pi/1/1 (40).gif', 
    '/home/pi/1/1 (41).gif', 
    '/home/pi/1/1 (42).gif', 
    '/home/pi/1/1 (43).gif', 
    '/home/pi/1/1 (44).gif', 
    '/home/pi/1/1 (45).gif', 
    '/home/pi/1/1 (46).gif', 
    '/home/pi/1/1 (47).gif', 
    '/home/pi/1/1 (48).gif', 
    '/home/pi/1/1 (49).gif', 
    '/home/pi/1/1 (50).gif', 
    '/home/pi/1/1 (51).gif', 
    '/home/pi/1/1 (52).gif', 
    '/home/pi/1/1 (53).gif', 
    '/home/pi/1/1 (54).gif', 
    '/home/pi/1/1 (55).gif', 
    '/home/pi/1/1 (56).gif', 
    '/home/pi/1/1 (57).gif', 
    '/home/pi/1/1 (58).gif', 
    '/home/pi/1/1 (59).gif', 
    '/home/pi/1/1 (60).gif', 
    '/home/pi/1/1 (61).gif', 
    '/home/pi/1/1 (62).gif', 
    '/home/pi/1/1 (63).gif', 
    '/home/pi/1/1 (64).gif', 
    '/home/pi/1/1 (65).gif', 
    '/home/pi/1/1 (66).gif', 
    '/home/pi/1/1 (67).gif', 
    '/home/pi/1/1 (68).gif', 
    '/home/pi/1/1 (69).gif', 
    '/home/pi/1/1 (70).gif', 
    '/home/pi/1/1 (71).gif', 
    '/home/pi/1/1 (72).gif', 
    '/home/pi/1/1 (73).gif',] 
    pictures = it.cycle(Tkinter.PhotoImage(file=img_name) for img_name in fname_list) 
    # milliseconds 
    if batch==1: 
     delay=10 
    else: 
     delay = 40*batchcount 
    animate() 
    logo7=PhotoImage(file="/home/pi/New2/close.gif") 
    button1 = Button(wt, width = 30, height = 30, image=logo7,command=wt.destroy) 
    button1.place(relx=0, x=445, y=0) 
    wt.mainloop() 

def batch_two_big(wt): 
    def ext(): 
     wt.destroy() 
    GPIO.output(40,GPIO.LOW) 
    sleep(1) 
    os.system("raspistill -o /home/pi/python/%s.jpeg -t 1000 -cfx 128:128 -co 100" %batchcount) 
    GPIO.output(40,GPIO.HIGH) 
    wt.after(100,ext) 
    winmain() 


def batchcallback1(): 
    wt=Tkinter.Toplevel() 
    t1=threading.Thread(target=batch_two_big,args=(wt,)) 
    t1.start() 
    waiting2(wt) 
    t1.join() 

def winmain(): 
    def ext(): 
     board1.destroy() 
    def jump(): 
     board1.after(100,ext) 
     batchcallback1() 

    board1 = Tkinter.Toplevel() 
    board1.title("PATIENT DETAILS") 
    board1.minsize(480,320) 
    logo8=PhotoImage(file="/home/pi/New2/read.gif") 
    board1.attributes('-fullscreen',True) 
    Label(board1, text="SAMPLE ID").place(relx=0, x=40, y=60) 
    e1=Entry(board1) 
    e1.place(relx=0, x=130, y=60) 
    b1=Button(board1,text="READ",image=logo8,bd=5,width=100,height=20,compound='left',fg='black',font='Times 12 bold', command=jump) 
    b1.image=logo8 
    b1.place(relx=0, x=80, y=5) 
    board1.mainloop() 
winmain() 
+0

向我们展示代码,否则我们无法帮助,无论如何只是阅读您的描述,在代码中的某些内容真的很糟糕,您在线程中递归调用了什么? – Netwave

+0

尊敬的Daniel Sanchez先生,正如我上面所说,主窗口有一个按钮,当我按下它时,用户界面显示进度条,并在后台通过os.system执行raspistill命令。 raspistill命令用于通过相机拍摄照片。我正在使用raspbian wheezhy发行版的Raspberry pi 2(单板嵌入式Linux计算机,包含1GB RAM内存)。当我平行监控免费的RAM空间时,每按一下按钮,RAM的内存就减少8MB。一旦ram达到50mb以下,就会显示上述错误。 –

回答

0

好吧,你有一个圆形的递归调用:

def batch_two_big(wt): 
    def ext(): 
     wt.destroy() 
    GPIO.output(40,GPIO.LOW) 
    sleep(1) 
    os.system("raspistill -o /home/pi/python/%s.jpeg -t 1000 -cfx 128:128 -co 100" %batchcount) 
    GPIO.output(40,GPIO.HIGH) 
    wt.after(100,ext) 
    winmain() 


def batchcallback1(): 
    wt=Tkinter.Toplevel() 
    t1=threading.Thread(target=batch_two_big,args=(wt,)) 
    t1.start() 
    waiting2(wt) 
    t1.join() 

def winmain(): 
    def ext(): 
     board1.destroy() 
    def jump(): 
     board1.after(100,ext) 
     batchcallback1() 

所以,winmain结合调用jump的按钮,然后在跳你打电话batchcallback1至极调用batchcallback1至极呼叫winmain再次重启循环。 尽量不要从batch_two_big调用winmain

def batch_two_big(wt): 
    GPIO.output(40,GPIO.LOW) 
    sleep(1) 
    os.system("raspistill -o /home/pi/python/%s.jpeg -t 1000 -cfx 128:128 -co 100" %batchcount) 
    GPIO.output(40,GPIO.HIGH) 
    wt.after(100,ext) 

不管怎样,使得一个线程在batchcallback1看起来没用,因为你只是等待它。

+0

是的,但是对于每次调用batchcallback1,它会创建线程对象t1,并且对于每次迭代,它将从ram中消耗8mb。我试过del t1命令,但不起作用。 –

+0

@SriKrishnan通过构建一个新的线程来创建自己的堆栈,所以消耗的RAM将永远存在,我想你可能不想创建它们或看看corrutines。 – Netwave

相关问题