如何通过python线程减少ram消耗

时间:2017-02-27 07:19:09

标签: python multithreading raspberry-pi2

我目前正在Raspberry pi 2中开发一个基于python的应用程序,它有1GB内存。

在我的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()

1 个答案:

答案 0 :(得分: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并重新启动循环。 请勿尝试从winmain调用batch_two_big

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中创建一个线程看起来毫无用处,因为你只是等待它。