1

Essentially, I have a GUI based program (Tkinter) and I am using multiple threads to update the values and do other stuff. With this, I have a global variable I want to share between threads, as one will update the changing value (thread1) and the other will use that same value to do its own thing in some loops in thread2. With thread2, I want this to run forever, constantly checking this value to the set number of <40 or >40. I've determined that the increase in memory comes from thread2, as commenting the thread out makes the program run without a significant memory increase. and it has nothing to do with the vlc objects, to me it seems like its the global variable or it being in an infinite loop.

Here is simplified code and the full code for thread2. I plan on running this on a rpi and over 2 minutes it gains about 1GB of consumed memory.

Any help is greatly appriciated. Thanks!!

(import vlc and others)
globalVar = 0.0

def Thread1():
    global globalVar
    #...modifies globalVar and GUI values

def Thread2():
    nowPlaying = vlc.MediaPlayer("song1.mp3")
    nowPlaying.play()
    changePlaying = vlc.MediaPlayer("song2.mp3")

    while True:
        extra = 0
        caseNum = random.randint(1,3)
        if caseNum == 1:
            eurobeat = vlc.MediaPlayer("song4.mp3")
        elif caseNum == 2:
            eurobeat = vlc.MediaPlayer("song5.mp3")
        elif caseNum == 3:
            eurobeat = vlc.MediaPlayer("song6.mp3")



        while globalVar >= 40:
            extra = extra + 1
            nowPlaying.set_pause(1)

            if (changePlaying.is_playing()) == 0:
                changePlaying.play()
                if extra > 2:
                    time.sleep(3)
                time.sleep(3)

            if globalVar < 40:
                nowPlaying.set_pause(0)
                changePlaying.stop()
            changePlaying.stop()

thread1 = Thread(target=Thread1)
thread1.setDaemon(True)
thread1.start()

thread2 = Thread(target=Thread2)
thread2.setDaemon(True)
thread2.start()

(tkinter)
root.mainloop()
user255580
  • 49
  • 4
  • 1
    I'm a bit suspicious of the constant allocation of new `vlc.MediaPlayer` objects, myself -- they might be quite heavyweight, and perhaps not garbage-collected as often as you might like. If possible, I'd suggest just allocating one or two `vlc.MediaPlayer` objects and then re-using them to play different media-files as necessary. – Jeremy Friesner Aug 22 '19 at 02:18
  • @JeremyFriesner, okay, Ill take a look into that tomorrow! Thanks for the idea. – user255580 Aug 22 '19 at 03:14
  • @JeremyFriesner, that section is the culprit. But I can't seem to figure out how to fix it. I want to create a mediaplayer object and then everytime it loops, I want it to randomly pick a different song, (hence the if else block). I keep getting an error with the set_media() where im passing in a string for the mp3 name/directory, and I get this:`TypeError: get_media() takes exactly 1 argument (2 given)` which the [documentation](https://www.olivieraubert.net/vlc/python-ctypes/doc/) says to pass in self,p_md, which is the media,If you have any ideas that would be greatly appriciated, Thanks – user255580 Aug 23 '19 at 02:19
  • Looks to me like the calls would be something like `eurobeat.set_media("song6.mp3")` and/or `curMedia = eurobeat.get_media()` – Jeremy Friesner Aug 23 '19 at 02:27

0 Answers0