Skip to content

Commit

Permalink
fix: Resolve RuntimeError by managing asyncio event loop in thread
Browse files Browse the repository at this point in the history
  • Loading branch information
cschmittiey committed Feb 12, 2025
1 parent 209567f commit 881fc0f
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ def mqtt_on_message(client, userdata, msg):


def mqtt_handle_message(filename):
# Transcribe audio if the talkgroup is one we're interested in
talkgroup = int(filename.split("-")[0])

if check_tg(talkgroup):
Expand All @@ -52,8 +51,15 @@ def mqtt_handle_message(filename):

# fire off discord notification unless there's no transcription text
if fulltext:
asyncio.run(send_discord_webhook(talkgroup, filename, fulltext))
logging.info(f"Transcription of {filename}: {fulltext}")
try:
# Set up a new event loop for this thread
new_loop = asyncio.new_event_loop()
asyncio.set_event_loop(new_loop)
new_loop.run_until_complete(send_discord_webhook(talkgroup, filename, fulltext))
new_loop.close()
logging.info(f"Transcription of {filename}: {fulltext}")
except Exception as e:
logging.error(f"Failed to send Discord webhook: {e}")

if not fulltext:
logging.info(f"Transcription of {filename}: No text found.")
Expand Down

0 comments on commit 881fc0f

Please sign in to comment.