类的Python线程

2024-09-30 18:16:18 发布

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

我本打算贴一个例子,但我说去死吧,我只是把我的东西贴出来。对我放松点。我习惯了红宝石。Python对我来说是全新的。在

我有一个叫做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"

“地狱世界”从不打印到终端。它一直在我的课上循环。 如果这能让你感觉好一点,你可以清理我的任何难看的命令。记住我是新来的。我已经在这里呆了几个小时了,只好问了。在


Tags: 文件selfcmdinitosdefopen歌曲
1条回答
网友
1楼 · 发布于 2024-09-30 18:16:18

循环不是从player.start()行开始的,而是从以下行开始:

player = MusicPlayer()

这是因为您在__init__中调用了self.playQue()。如果删除该行,并将方法playQue更改为run,则该线程应该单独运行。在

有关start和{}的说明,请参见instructions for the threading package

start()

Start the thread’s activity.

It must be called at most once per thread object. It arranges for the object’s run() method to be invoked in a separate thread of control.

相关问题 更多 >