如何减少python线程的ram消耗

2024-06-16 14:51:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在Raspberry Pi2中开发一个基于python的应用程序,它有1GB的ram。你知道吗

在我的python应用程序中,我使用线程概念来显示progressbar(线程将执行主进程,普通循环执行用户界面中的进度条)。你知道吗

每次执行线程时,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()

Tags: imagehomeimggpiotkinterdefpiplace
1条回答
网友
1楼 · 发布于 2024-06-16 14:51:39

好的,您有一个循环递归调用:

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

相关问题 更多 >