我打算髮表一個例子,但我說,擰它,只是發佈我有什麼,而不是。在我身上輕鬆一下。我習慣於使用紅寶石。 Python對我來說是全新的。Python的線程與類
我有一個名爲que的文件,它包含一堆歌曲。我想做一個後臺線程,不斷檢查是否que有任何歌曲。如果它有歌曲,則在第一行播放歌曲,然後刪除第一行。 (.que.swp)。
現在的問題是,我不知道如何做到這一切在後臺。我有另一個類,允許用戶添加歌曲到que文件。所以他們需要同時運行。
class MusicPlayer(threading.Thread):
def __init__(self):
super(MusicPlayer, self).__init__()
self.que_file = "que"
self.playQue()
def playQue(self):
while 1:
try:
f = open(self.que_file, "r")
songUp = f.readline()
songUp = songUp.rstrip()
cmd = "cvlc \"%s\" vlc://quit &>/dev/null" % (songUp)
os.system(cmd)
data="".join(open(self.que_file).readlines()[1:-1])
open(".que.swp","wb").write(data)
os.remove(self.que_file)
os.rename(".que.swp", self.que_file)
print "\n%s added %s to the que" % (self.user, self.dir)
except:
print "No Que File Exists"
time.sleep(1)
#main#
if __name__ == '__main__':
player = MusicPlayer()
player.start()
print "helloWorld"
「helloworld」從不打印到終端。它只是不斷循環我的課程。 ps - 如果它讓你感覺更好,你可以清理我的任何醜陋的命令。記得我是新手。我已經在這幾個小時,並已經訴諸問道。
謝謝你!那讓我瘋狂。我其實不知道這就是運行方法的原因。明確地指出未來 –