Skip to content

Commit

Permalink
Merge pull request #3 from speechmatics/fix-async-for
Browse files Browse the repository at this point in the history
Better logging of exceptions from async tasks
  • Loading branch information
anjz authored Mar 27, 2024
2 parents fa70f06 + d4898b0 commit 8a042dc
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions stream_transcriber/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,29 +241,29 @@ async def load_stream(stream_name: str):
"Starting transcription",
extra={"stream": stream_name},
)
asr_task = asyncio.create_task(sm_client.run(runtime_stream, conf, settings))
send_audio_task = asyncio.create_task(send_audio(broadcast_stream, stream_name))
log_task = asyncio.create_task(log_ffmpeg(process))
asr_task = asyncio.create_task(
sm_client.run(runtime_stream, conf, settings), name="asr_task"
)
send_audio_task = asyncio.create_task(
send_audio(broadcast_stream, stream_name), name="send_audio_task"
)
log_task = asyncio.create_task(log_ffmpeg(process), name="log_task")

done, pending = await asyncio.wait(
done, _ = await asyncio.wait(
[log_task, send_audio_task, asr_task, stream_clone_task],
return_when=asyncio.FIRST_EXCEPTION,
)

for done_routine in done:
if done_routine.exception() is not None:
exception_name = type(done_routine.exception()).__name__
LOGGER.error(
"Exception in return %s",
"Exception for done routine: %s. Exception (%s): %s",
done_routine.get_name(),
exception_name,
done_routine.exception(),
extra={"stream": stream_name},
)
for pending_routine in pending:
pending_routine.cancel()
if pending_routine.exception() is not None:
LOGGER.error(
"Exception in return %s",
pending_routine.exception(),
extra={"stream": stream_name},
)

except asyncio.CancelledError:
LOGGER.warning("Task Cancelled", extra={"stream": stream_name})
Expand Down

0 comments on commit 8a042dc

Please sign in to comment.