如何让我的代码的一部分等待另一个函数返回布尔值然后继续执行?

问题描述 投票:0回答:0

所以我正在使用 discord.py 库 在 python 中制作一个 discord 音乐机器人。我想通过创建一个函数槽来创建一个歌曲队列,所请求歌曲的源必须通过该函数槽传递。该函数使用 if vc.is_playing() == True:

 检查客户端当前是否正在播放任何音频,如果是,则应等到 vc.is_playing() == False:
 。音频播放完毕后,该函数将返回源并开始播放另一首歌曲。
问题是:

怎样才能让一部分代码等待另一部分执行完毕

(完成播放音频)然后继续正常执行? 这是应该进行等待的

不完整函数

的代码: async def wait_for_url(self, ctx, source, title): vc = ctx.voice_client while True: if vc.is_playing() == True: print("@Inside function - 1!@") **# Something that waits for client to stop playing should go here!** print("@Inside function - 2 - waiting should be over!@") time.sleep(5) url_final_u = source title_u = title print("@Inside function - 3 - returning!@") return url_final_u and title_u else: print("@Inside function - A - else condition met!@") url_final_u = source title_u = title return url_final_u and title_u

我还将提供我的
/play

命令

部分的代码,其中应实现上述功能:
vc = ctx.voice_client self.queue_list.append(url_final_u) await ctx.send(f"{title_u} has been added to the queue!") if len(self.queue_list) > 0: print("@Before function!") await self.wait_for_url(ctx, url_final_u, title_u) ** # <--- This is the function in question.** print("@After function!@") if vc.is_playing() == False: time.sleep(5) await self.play_music(ctx, url_final_u, title_u)

所有这些代码都在 
class commands(commands.Cog)

齿轮内。

我尝试研究 

os.wait()

函数,甚至尝试询问

GPT 3.5
,它建议使用 threading
 
module
及其 .wait() 函数并编写了示例代码。但是当我尝试实现它时,它只是没有等待客户端停止播放。
也就是说,如果我正确使用了

threading

,因为我对编程还很陌生,可能只是错过了一些非常明显的东西,所以我会很高兴地欢迎每一个可以帮助我解决这个问题的建议。

python function discord.py bots wait
© www.soinside.com 2019 - 2024. All rights reserved.