Refactor loop code to allow usage of asyncio.run

This commit is contained in:
Han Seung Min - 한승민
2022-03-13 14:24:14 +05:30
committed by GitHub
parent 196db33e9f
commit 93af158b0c
9 changed files with 44 additions and 136 deletions

View File

@ -521,9 +521,9 @@ class FFmpegOpusAudio(FFmpegAudio):
raise TypeError(f"Expected str or callable for parameter 'probe', not '{method.__class__.__name__}'")
codec = bitrate = None
loop = asyncio.get_event_loop()
loop = asyncio.get_running_loop()
try:
codec, bitrate = await loop.run_in_executor(None, lambda: probefunc(source, executable)) # type: ignore
codec, bitrate = await loop.run_in_executor(None, lambda: probefunc(source, executable))
except Exception:
if not fallback:
_log.exception("Probe '%s' using '%s' failed", method, executable)
@ -531,7 +531,7 @@ class FFmpegOpusAudio(FFmpegAudio):
_log.exception("Probe '%s' using '%s' failed, trying fallback", method, executable)
try:
codec, bitrate = await loop.run_in_executor(None, lambda: fallback(source, executable)) # type: ignore
codec, bitrate = await loop.run_in_executor(None, lambda: fallback(source, executable))
except Exception:
_log.exception("Fallback probe using '%s' failed", executable)
else:
@ -744,6 +744,6 @@ class AudioPlayer(threading.Thread):
def _speak(self, speaking: SpeakingState) -> None:
try:
asyncio.run_coroutine_threadsafe(self.client.ws.speak(speaking), self.client.loop)
asyncio.run_coroutine_threadsafe(self.client.ws.speak(speaking), self.client.client.loop)
except Exception as e:
_log.info("Speaking call in player failed: %s", e)