Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
439 views
in Technique[技术] by (71.8m points)

python - memory_profiler while using lambda expression to connect slots

I am experimenting memory_profiler in Python3 following

https://medium.com/zendesk-engineering/hunting-for-memory-leaks-in-python-applications-6824d0518774

thanks to @eyllanesc here PyQt5 designer gui and iterate/loop over QPushButton [duplicate]

I just created a mainwindow with 21 buttons a to z ; each time a press one of them I print the letter they represent.

While reading: Using lambda expression to connect slots in pyqt I came across:

" Beware! As soon as you connect your signal to a lambda slot with a reference to self, your widget will not be garbage-collected! That's because lambda creates a closure with yet another uncollectable reference to the widget.

Thus, self.someUIwidget.someSignal.connect(lambda p: self.someMethod(p)) is very evil :) "

Here my plot: memory_profiler plot while pressing buttons

while pressing buttons.

Does my plot shows this behaviour ?? Or is it a straight line that doent looks straight ?

What is an alternative then? to my:

use for letter in "ABCDE": getattr(self, letter).clicked.connect(lambda checked, letter=letter: foo(letter))

main.py : hangman_pyqt5-muppy.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May  5 19:21:27 2020

@author: Pietro

"""

import sys

from PyQt5 import QtWidgets, uic

from PyQt5.QtWidgets import QDesktopWidget

import hangman005

#from pympler import muppy, summary #########################################
#
#from pympler import tracker

#import resource


WORDLIST_FILENAME = "words.txt"

wordlist = hangman005.load_words(WORDLIST_FILENAME)



def main():


    def center(self):                     
        qr = self.frameGeometry()   
        cp = QDesktopWidget().availableGeometry().center()    
        qr.moveCenter(cp)    
        self.move(qr.topLeft())


    class MainMenu(QtWidgets.QMainWindow):


        def __init__(self):        
            super(MainMenu, self).__init__()            
            uic.loadUi('main_window2.ui', self)                       
#                self.ButtonQ.clicked.connect(self.QPushButtonQPressed)             
            self.centro = center(self)             
            self.centro                      
#            self.show() 
            self.hangman()



        def closeEvent(self, event): #Your desired functionality here         
            close = QtWidgets.QMessageBox.question(self,
                                         "QUIT",
                                         "Are you sure want to stop process?",
                                         QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No)
            if close == QtWidgets.QMessageBox.Yes:
                event.accept()
            else:
                event.ignore()

        def printo(self, i):
            print('oooooooooooooooooooooo :', i)   
#            all_objects = muppy.get_objects()
#            sum1 = summary.summarize(all_objects)# Prints out a summary of the large objects
#            summary.print_(sum1) from pympler import tracker
#           
#
#            self.memory_tracker = tracker.SummaryTracker()
#            self.memory_tracker.print_diff()

        def hangman(self):
            letters_guessed=[]
            max_guesses = 6
            secret_word = hangman005.choose_word(wordlist)
            secret_word_lenght=len(secret_word)
            secret_word=hangman005.splitt(secret_word)
            vowels=('a','e','i','o','u')
            secret_word_print=('_ '*secret_word_lenght )
            self.word_to_guess.setText(secret_word_print )
            letters=hangman005.splitt('ABCDEFGHIJKLMNOPQRSTYVWXXYZ')
            print(letters )
            for i in letters:
                print('lllllllllllllll : ' ,i)
#                 button = "self.MainMenu."+i
#                 self.[i].clicked.connect(self.printo(i))
#                 button = getattr(self, i)
#                button.clicked.connect((lambda : self.printo(i for i in letters) )    )
#                 button.clicked.connect(lambda j=self.printo(i) : j )
#                 button.clicked.connect(lambda : self.printo(i))
#                button.clicked.connect(lambda: self.printo(i))
#            button.clicked.connect(lambda j=self.printo(i) : j   for i in letters )
#                 getattr(self, i).clicked.connect(lambda checked, i=i: self.printo(i))
#                 getattr(self, i).clicked.connect(lambda checked, j=i: self.printo(j))
                getattr(self, i).clicked.connect(lambda pippo, j=i: self.printo(j))
#            self.A.clicked.connect(self.printo)
# Add to leaky code within python_script_being_profiled.py

                 # Get references to certain types of objects such as dataframe
#                dataframes = [ao for ao in all_objects if isinstance(ao, pd.DataFrame)]
#                
#                for d in dataframes:
#                    print (d.columns.values)
#                    print (len(d))                







    app = QtWidgets.QApplication(sys.argv)

#    sshFile="coffee.qss"
#    with open(sshFile,"r") as fh:
#        app.setStyleSheet(fh.read())

    window=MainMenu()
    window.show()

    app.exec_()

#all_objects = muppy.get_objects()
#sum1 = summary.summarize(all_objects)# Prints out a summary of the large objects
#summary.print_(sum1)# Get references to certain types of objects such as dataframe

if __name__ == '__main__':
    main()

hangman005.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 29 19:36:32 2020

@author: Pietro
"""



import random
import string



#WORDLIST_FILENAME = "words.txt"


def load_words(WORDLIST_FILENAME):
    """
    Returns a list of valid words. Words are strings of lowercase letters.

    Depending on the size of the word list, this function may
    take a while to finish.
    """
    print("Loading word list from file...")
    # inFile: file
    inFile = open(WORDLIST_FILENAME, 'r')
    # line: string
    line = inFile.readline()
    # wordlist: list of strings
    wordlist = line.split()
    print("  ", len(wordlist), "words loaded.")
#    print(line)
#    for elem in line:
#        print (elem)
#    print(wordlist)
#    for elem in wordlist:
#        print ('
' , elem) 
    return wordlist



def choose_word(wordlist):
    """
    wordlist (list): list of words (strings)

    Returns a word from wordlist at random
    """
    return random.choice(wordlist)

# end of helper code

# -----------------------------------

# Load the list of words into the variable wordlist
# so that it can be accessed from anywhere in the program

#wordlist = load_words()



def splitt(word): 
    return [char for char in word]   


def is_word_guessed(secret_word, letters_guessed):
    '''
    secret_word: string, the word the user is guessing; assumes all letters are
      lowercase
    letters_guessed: list (of letters), which letters have been guessed so far;
      assumes that all letters are lowercase
    returns: boolean, True if all the letters of secret_word are in letters_guessed;
      False otherwise
    '''
    prova =all(item in letters_guessed for item in secret_word )    
    print(' prova : ' , prova )
    return prova 

#secret_word='popoli'
#letters_guessed=['p','o','p','o','l','i']
#letters_guessed=['p','i','l']    
#print('

is_word_guessed : ', is_word_guessed(secret_word,letters_guessed))
#letters_guessed = []


def get_guessed_word(secret_word, letters_guessed):
    '''
    secret_word: string, the word the user is guessing
    letters_guessed: list (of letters), which letters have been guessed so far
    returns: string, comprised of letters, underscores (_), and spaces that represents
      which letters in secret_word have been guessed so far.
    '''
    print('

secret_word_split' , secret_word)
    print('letters_guessed', letters_guessed )
    results=[]
    for val in range(0,len(secret_word)):
            if secret_word[val] in letters_guessed:
                results.append(secret_word[val])
            else:
                results.append('_')
    print('
results : ' , ' '.join(results ))        
    return results


def get_available_letters(letters_guessed):
    '''secret_word_split
    letters_guessed: list (of letters), which letters have been guessed so far
    returns: string (of letters), comprised of letters that represents which letters have not
      yet been guessed.
    '''
    entire_letters='abcdefghijklmnopqrstuvwxyz'
    entire_letters_split=splitt(entire_letters)
    entire_letters_split = [x for x in entire_letters_split if x not in letters_guessed]
    return entire_letters_split




def hangman(secret_word):
    '''
    secret_word: string, the secret word to guess.

    Starts up an interactive game of Hangman.

    * At the start of the game, let the user know how many 
      letters the secret_word contains and how many guesses s/he starts with.

    * The user should start with 6 guesses

    * Before each round, you should display to the user how many guesses
      s/he has left and the letters that the user has not yet guessed.

    * Ask the user to supply one guess per round. Remember to make
      sure that the user puts in a letter!

    * The user should receive feedback immediately after each guess 
      about whether their guess appears in the computer's word.

    * After each guess, you should display to the user the 
      partially guessed word so far.
    secret_word_split
    Follows the other limitations detailed in the problem write-up.
    '''

    letters_guessed=[]
    max_guesses = 6
    secret_word_lenght=len(secret_word)
    secret_word=splitt(secret_word)
    vowels=('a','e','i','o','u')

    print('
Welcome to HANGMAN ;-) ')
    print('
secret_word_lenght : ' , secret_word_lenght  ) 
    print('
'+' _ '*secret_word_lenght )
    print('
you have ' , max_guesses , ' guesses be carefull choosing')

    while True:
        guess= input('
make your first choice : ' )
        if guess not in get_available_letters(letters_guessed):
                print('You can only choose in' , ' '.join(get_available_letters(letters_guessed)))
                continue
        if guess in get_available_letters(letters_guessed):
                letters_guessed.append(guess)
#                print('
letters_guessed appended : ' , ' '.join(letters_guessed) )
#                max_guesses -= 1
                print(' a che punto sei : ' , ' '.join(get_guessed_word(secret_word, letters_guessed)))
#                print('
you have ' , max_guesses , ' guesses be carefull choosing')
                if guess in secret_word:
                    print('GOOD !!!!!!!!!!!!!')
                    print('
you still have ' , max_guesses , ' guesses be carefull choosing')
                else:
                    print('ERRORE !!!!!!!!!!!!!!!!!!!!!!')
                    if guess in vowels:
                        max_guesses -= 2
                    else:
                        max_guesses -= 1
                    print('
now you 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem pointed out in this post does not have to do with PyQt5 but with the lambda functions since, like any function, it creates a scope and stores memory, to test what the OP points out I have created the following example:

from PyQt5 import QtCore, QtWidgets


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.counter = 0
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.on_timeout)
        self.timer.start(1000)

        self.button = QtWidgets.QPushButton("Press me")
        self.setCentralWidget(self.button)

    @QtCore.pyqtSlot()
    def on_timeout(self):
        self.counter += 1
        if self.counter % 2 == 0:
            self.connect()
        else:
            self.disconnect()

    def connect(self):
        self.button.setText("Connected")
        self.button.clicked.connect(lambda checked: None)
        # or
        # self.button.clicked.connect(lambda checked, v=list(range(100000)): None)

    def disconnect(self):
        self.button.setText("Disconnected")
        self.button.disconnect()


def main():
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()
  • self.button.clicked.connect(lambda checked: None)

enter image description here

  • self.button.clicked.connect(lambda checked, v=list(range(100000)): None)

enter image description here

As observed at the time of connection and disconnection, the memory consumed increases since the lambda maintains the information of v=list(range(100000)).


But in your code the closure stores only the variable "j" which is minimum:

getattr(self, i).clicked.connect(lambda pippo, j=i: self.printo(j))

To see how this variable affects I am going to eliminate the unnecessary code for the test (hangman005.py, etc) in addition to offering alternatives:

  • without connect:
class MainMenu(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainMenu, self).__init__()
        uic.loadUi("main_window2.ui", self)
        self.hangman()

    def hangman(self):
        letters = "ABCDEFGHIJKLMNOPQRSTYVWXXYZ"
        for i in letters:
            pass

    def printo(self, i):
        print("oooooooooooooooooooooo :", i)

enter image description here

  • with lambda:
class MainMenu(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainMenu, self).__init__()
        uic.loadUi("main_window2.ui", self)
        self.hangman()

    def hangman(self):
        letters = "ABCDEFGHIJKLMNOPQRSTYVWXXYZ"
        for i in letters:
            getattr(self, i).clicked.connect(lambda pippo, j=i: self.printo(j))

    def printo(self, i):
        print("oooooooooooooooooooooo :", i)

enter image description here

  • with functools.partial
class MainMenu(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainMenu, self).__init__()
        uic.loadUi("main_window2.ui", self)
        self.hangman()

    def hangman(self):
        letters = "ABCDEFGHIJKLMNOPQRSTYVWXXYZ"
        for i in letters:
            getattr(self, i).clicked.connect(partial(self.printo, i))

    def printo(self, i):
        print("oooooooooooooooooooooo :", i)

enter image description here

  • once connect
class MainMenu(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainMenu, self).__init__()
        uic.loadUi("main_window2.ui", self)
        self.hangman()

    def hangman(self):
        letters = "ABCDEFGHIJKLMNOPQRSTYVWXXYZ"
        for i in letters:
            getattr(self, i).setProperty("i", i)
            getattr(self, i).clicked.connect(self.printo)

    def printo(self):
        i = self.sender().property("i")
        print("oooooooooooooooooooooo :", i)

enter image description here

As we can see there is no significant difference between all the methods so in your case there is no memory leak or rather it is very small.

In conclusion: Every time you create a lambda method it has a closure (j = i in your case) so the OP recommends taking that into account, for example in your case len(letters) * size_of(i) are consumed which at Being small letters and i makes it negligible, but if you otherwise store a heavier object unnecessarily then it will cause problems


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...