From 2f26328e2224a4bd9530e9fa6423e5b35c2b8279 Mon Sep 17 00:00:00 2001 From: Tonya Camille Date: Tue, 15 Mar 2022 09:52:28 -0500 Subject: [PATCH 1/2] Initial commit to fix continuous streaming issue --- .gitignore | 5 ++++- deepgram/_version.py | 2 +- deepgram/transcription.py | 22 ++++++++++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) 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..f599390b 100644 --- a/deepgram/transcription.py +++ b/deepgram/transcription.py @@ -10,6 +10,7 @@ LiveTranscriptionResponse, Metadata, EventHandler) from ._enums import LiveTranscriptionEvent from ._utils import _request, _make_query_string, _socket_connect +import traceback class PrerecordedTranscription: @@ -41,6 +42,8 @@ async def __call__( class LiveTranscription: _root = "/listen" + MESSAGE_TIMEOUT = 1.0 + RETRY = 4 def __init__(self, options: Options, transcription_options: LiveOptions) -> None: @@ -65,8 +68,22 @@ async def __call__(self) -> 'LiveTranscription': async def _start(self) -> None: asyncio.create_task(self._receiver()) self._ping_handlers(LiveTranscriptionEvent.OPEN, self) + + connection_retries = 0 + 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: + print(f"Server didn't receive or send message within {self.MESSAGE_TIMEOUT} seconds") + + if connection_retries < self.RETRY: + connection_retries += 1 + continue + + self.done = True + break + if incoming: try: parsed: Union[ @@ -98,7 +115,8 @@ 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 + traceback.print_exc() + self.done = True # socket closed, will terminate on next loop def _ping_handlers(self, event_type: LiveTranscriptionEvent, body: Any) -> None: From 647b3c8726d8e3526dc16d56b9cbf43b92555228 Mon Sep 17 00:00:00 2001 From: Tonya Camille Date: Tue, 15 Mar 2022 15:52:36 -0500 Subject: [PATCH 2/2] Update transcription.py with suggested changes --- deepgram/transcription.py | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/deepgram/transcription.py b/deepgram/transcription.py index f599390b..836ba9c5 100644 --- a/deepgram/transcription.py +++ b/deepgram/transcription.py @@ -10,7 +10,6 @@ LiveTranscriptionResponse, Metadata, EventHandler) from ._enums import LiveTranscriptionEvent from ._utils import _request, _make_query_string, _socket_connect -import traceback class PrerecordedTranscription: @@ -43,7 +42,6 @@ async def __call__( class LiveTranscription: _root = "/listen" MESSAGE_TIMEOUT = 1.0 - RETRY = 4 def __init__(self, options: Options, transcription_options: LiveOptions) -> None: @@ -69,20 +67,14 @@ async def _start(self) -> None: asyncio.create_task(self._receiver()) self._ping_handlers(LiveTranscriptionEvent.OPEN, self) - connection_retries = 0 - while not self.done: try: incoming, body = await asyncio.wait_for(self._queue.get(), self.MESSAGE_TIMEOUT) except asyncio.TimeoutError: - print(f"Server didn't receive or send message within {self.MESSAGE_TIMEOUT} seconds") - - if connection_retries < self.RETRY: - connection_retries += 1 - continue - - self.done = True - break + if self._socket.closed: + self.done = True + break + continue if incoming: try: @@ -115,7 +107,6 @@ async def _receiver(self) -> None: body = await self._socket.recv() self._queue.put_nowait((True, body)) except Exception as exc: - traceback.print_exc() self.done = True # socket closed, will terminate on next loop def _ping_handlers(self, event_type: LiveTranscriptionEvent,