【发布时间】:2018-01-20 23:01:23
【问题描述】:
我有一个函数可以在单独的线程中循环播放声音文件(取自this question 的答案),我的函数将它应该播放的文件的名称作为参数。
def loop_play(sound_file):
audio = AudioSegment.from_file(sound_file)
while is_playing:
play(audio)
def play_sound(sound_file):
global is_playing
global sound_thread
if not is_playing:
is_playing = True
sound_thread = Thread(target=loop_play,args=[sound_file])
sound_thread.daemon = True
sound_thread.start()
每次调用play_sound 时,我都会覆盖sound_thread 并创建一个新线程。旧的会怎样?它还在后台运行吗?有没有办法终止它?
【问题讨论】:
标签: python multithreading