diff --git a/.gitignore b/.gitignore index fbeb1c04..39548434 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ *.egg-info */__pycache__/ -dist/ \ No newline at end of file +dist/ +.venv +venv/ +venv.bak/ \ No newline at end of file diff --git a/deepgram/_version.py b/deepgram/_version.py index 13a85f77..44b18069 100644 --- a/deepgram/_version.py +++ b/deepgram/_version.py @@ -1 +1 @@ -__version__ = '0.2.5' +__version__ = '0.2.6' diff --git a/deepgram/transcription.py b/deepgram/transcription.py index 5537d198..836ba9c5 100644 --- a/deepgram/transcription.py +++ b/deepgram/transcription.py @@ -41,6 +41,7 @@ async def __call__( class LiveTranscription: _root = "/listen" + MESSAGE_TIMEOUT = 1.0 def __init__(self, options: Options, transcription_options: LiveOptions) -> None: @@ -65,8 +66,16 @@ async def __call__(self) -> 'LiveTranscription': async def _start(self) -> None: asyncio.create_task(self._receiver()) self._ping_handlers(LiveTranscriptionEvent.OPEN, self) + while not self.done: - incoming, body = await self._queue.get() + try: + incoming, body = await asyncio.wait_for(self._queue.get(), self.MESSAGE_TIMEOUT) + except asyncio.TimeoutError: + if self._socket.closed: + self.done = True + break + continue + if incoming: try: parsed: Union[ @@ -98,7 +107,7 @@ async def _receiver(self) -> None: body = await self._socket.recv() self._queue.put_nowait((True, body)) except Exception as exc: - pass # socket closed, will terminate on next loop + self.done = True # socket closed, will terminate on next loop def _ping_handlers(self, event_type: LiveTranscriptionEvent, body: Any) -> None: