(python) Different behaviour when called using threading.Timer

101 Views Asked by At

I want to make a little command line music player based on the python module "mp3play". I want to check regularly, if a song has stopped playing (and eventually start a new song), but the user should be able to type new commands during that time (like pausing the music). Therefor i tried to use threading.Timer for that. However, it gives me an error if i am inside the function that was called using the timer. the error does not occur when the function was called normally. Heres my (reduced) code:

from threading import Timer
global currentmusic 

def rep():
    b = currentmusic.isplaying() #this is where the error occurs
    if b:
        print "Music is playing"
    else:
        print "Music has stopped"
    t=Timer(5.0,rep) #repeat every 5 seconds
    t.start()
currentmusic=playrandomfile() #loads a song and starts playing it
rep() #call the first time

When rep() is called the second time, it gives me an MCI error in the function isplaying(), saying that it cannot read the device. My questions:
Am i making a mistake with the way the threading.Timer works? (and how can i fix it?) Is there another way than threading.Timer to achieve the stuff i want?
My thoughts so far were, that it could be a problem to access currentmusic from another thread, but i am not sure. Also i dont know how to avoid it.
Thx for helping

1

There are 1 best solutions below

0
On

Ive used mp3play in some projects, and it works fine for me. IMO recursion threading is the problem. Just remove the threading timer and leave the rep function call, it wont lag any pc. You should use threading only for raw_input.