Skip to content

Commit 4c9c639

Browse files
Fix Issue 264 - Using Shared WebSocket Class Implementation
1 parent c2b0790 commit 4c9c639

File tree

30 files changed

+1876
-2034
lines changed

30 files changed

+1876
-2034
lines changed

deepgram/audio/speaker/speaker.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from typing import Optional, Callable, Union, TYPE_CHECKING
1010
import logging
1111

12+
import websockets
13+
1214
from ...utils import verboselogs
1315
from .constants import LOGGING, CHANNELS, RATE, CHUNK, TIMEOUT
1416

@@ -24,22 +26,22 @@ class Speaker: # pylint: disable=too-many-instance-attributes
2426
_logger: verboselogs.VerboseLogger
2527

2628
_audio: "pyaudio.PyAudio"
27-
_stream: "pyaudio.Stream"
29+
_stream: Optional["pyaudio.Stream"] = None
2830

2931
_chunk: int
3032
_rate: int
3133
_channels: int
32-
_output_device_index: Optional[int]
34+
_output_device_index: Optional[int] = None
3335

3436
_queue: queue.Queue
3537
_exit: threading.Event
3638

37-
_thread: threading.Thread
39+
_thread: Optional[threading.Thread] = None
3840
# _asyncio_loop: asyncio.AbstractEventLoop
3941
# _asyncio_thread: threading.Thread
40-
_receiver_thread: threading.Thread
42+
_receiver_thread: Optional[threading.Thread] = None
4143

42-
_loop: asyncio.AbstractEventLoop
44+
_loop: Optional[asyncio.AbstractEventLoop] = None
4345

4446
_push_callback_org: Optional[Callable] = None
4547
_push_callback: Optional[Callable] = None
@@ -217,6 +219,17 @@ async def _start_asyncio_receiver(self):
217219
elif isinstance(message, bytes):
218220
self._logger.verbose("Received audio data...")
219221
self.add_audio_to_queue(message)
222+
except websockets.exceptions.ConnectionClosedOK as e:
223+
self._logger.debug("send() exiting gracefully: %d", e.code)
224+
except websockets.exceptions.ConnectionClosed as e:
225+
if e.code in [1000, 1001]:
226+
self._logger.debug("send() exiting gracefully: %d", e.code)
227+
return
228+
self._logger.error("_start_asyncio_receiver - ConnectionClosed: %s", str(e))
229+
except websockets.exceptions.WebSocketException as e:
230+
self._logger.error(
231+
"_start_asyncio_receiver- WebSocketException: %s", str(e)
232+
)
220233
except Exception as e: # pylint: disable=broad-except
221234
self._logger.error("_start_asyncio_receiver exception: %s", str(e))
222235

@@ -266,23 +279,26 @@ def finish(self) -> bool:
266279
self._logger.notice("stopping stream...")
267280
self._stream.stop_stream()
268281
self._stream.close()
269-
self._stream = None # type: ignore
282+
self._stream = None
270283
self._logger.notice("stream stopped")
271284

272-
self._thread.join()
273-
self._thread = None # type: ignore
285+
if self._thread is not None:
286+
self._logger.notice("joining thread...")
287+
self._thread.join()
288+
self._thread = None
289+
self._logger.notice("thread stopped")
274290

275291
# if self._asyncio_thread is not None:
276292
# self._logger.notice("stopping asyncio loop...")
277293
# self._asyncio_loop.call_soon_threadsafe(self._asyncio_loop.stop)
278294
# self._asyncio_thread.join()
279-
# self._asyncio_thread = None # type: ignore
295+
# self._asyncio_thread = None
280296
# self._logger.notice("_asyncio_thread joined")
281297

282298
if self._receiver_thread is not None:
283299
self._logger.notice("stopping asyncio loop...")
284300
self._receiver_thread.join()
285-
self._receiver_thread = None # type: ignore
301+
self._receiver_thread = None
286302
self._logger.notice("_receiver_thread joined")
287303

288304
self._queue = None # type: ignore

deepgram/clients/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,13 @@
4040
UnhandledResponse,
4141
ErrorResponse,
4242
)
43-
from .common import DeepgramError, DeepgramTypeError
44-
from .errors import DeepgramModuleError, DeepgramApiError, DeepgramUnknownApiError
43+
from .common import (
44+
DeepgramError,
45+
DeepgramTypeError,
46+
DeepgramApiError,
47+
DeepgramUnknownApiError,
48+
)
49+
from .errors import DeepgramModuleError
4550

4651
from .listen_router import Listen
4752
from .read_router import Read

deepgram/clients/analyze/v1/async_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
from ....utils import verboselogs
1111
from ....options import DeepgramClientOptions
12-
from ...abstract_async_client import AbstractAsyncRestClient
13-
from ...common.v1.errors import DeepgramError, DeepgramTypeError
12+
from ...common import AbstractAsyncRestClient
13+
from ...common import DeepgramError, DeepgramTypeError
1414

1515
from .helpers import is_buffer_source, is_readstream_source, is_url_source
1616
from .options import (

deepgram/clients/analyze/v1/client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
from ....utils import verboselogs
1111
from ....options import DeepgramClientOptions
12-
from ...abstract_sync_client import AbstractSyncRestClient
13-
from ...common.v1.errors import DeepgramError, DeepgramTypeError
12+
from ...common import AbstractSyncRestClient
13+
from ...common import DeepgramError, DeepgramTypeError
1414

1515
from .helpers import is_buffer_source, is_readstream_source, is_url_source
1616
from .options import (

deepgram/clients/common/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
22
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
33
# SPDX-License-Identifier: MIT
44

5-
from .v1 import DeepgramError, DeepgramTypeError
5+
from .v1 import (
6+
DeepgramError,
7+
DeepgramTypeError,
8+
DeepgramApiError,
9+
DeepgramUnknownApiError,
10+
)
11+
12+
from .v1 import AbstractAsyncRestClient
13+
from .v1 import AbstractSyncRestClient
14+
from .v1 import AbstractAsyncWebSocketClient
15+
from .v1 import AbstractSyncWebSocketClient
616

717
from .v1 import (
818
TextSource as TextSourceLatest,

deepgram/clients/common/v1/__init__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@
44

55
from .enums import Sentiment
66

7-
from .errors import DeepgramError, DeepgramTypeError
7+
from .errors import (
8+
DeepgramError,
9+
DeepgramTypeError,
10+
DeepgramApiError,
11+
DeepgramUnknownApiError,
12+
)
13+
from .abstract_async_rest import AbstractAsyncRestClient
14+
from .abstract_sync_rest import AbstractSyncRestClient
15+
from .abstract_async_websocket import AbstractAsyncWebSocketClient
16+
from .abstract_sync_websocket import AbstractSyncWebSocketClient
817

918
from .options import (
1019
TextSource,

deepgram/clients/abstract_async_client.py renamed to deepgram/clients/common/v1/abstract_async_rest.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@
99
import httpx
1010

1111
from .helpers import append_query_params
12-
from ..options import DeepgramClientOptions
13-
from .errors import DeepgramApiError, DeepgramUnknownApiError
14-
from .common.v1.errors import DeepgramError
12+
from ....options import DeepgramClientOptions
13+
from .errors import DeepgramError, DeepgramApiError, DeepgramUnknownApiError
1514

1615

1716
class AbstractAsyncRestClient:

0 commit comments

Comments
 (0)