我是线程的新手,我不太了解如何使用条件。目前,我有一个像这样的线程类:

class MusicThread(threading.Thread):
    def __init__(self, song):
        threading.Thread.__init__(self)
        self.song = song
    def run(self):
        self.output = audiere.open_device()
        self.music = self.output.open_file(self.song, 1)
        self.music.play()
        #i want the thread to wait indefinitely at this point until
        #a condition/flag in the main thread is met/activated

在主线程中,相关代码为:
music = MusicThread(thesong)
music.start()

这应该意味着我可以通过辅助线程播放一首歌,直到我在主线程中发出命令来停止它。我猜我必须使用锁和 wait() 什么的?

最佳答案

这里有一个更简单的解决方案。您正在使用 Audiere 库,它已经在自己的线程中播放音频。因此,没有必要为了播放音频而创建自己的第二个线程。相反,直接从主线程使用 Audiere,并从主线程停止它。

关于python - 线程和条件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6480132/

10-11 22:57
查看更多