Skip to content

Commit 000d45c

Browse files
committed
Add threading.Thread Class Override
1 parent 7df7b27 commit 000d45c

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

deepgram/clients/common/v1/abstract_sync_websocket.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55
import time
66
import logging
7-
from typing import Dict, Union, Optional, cast, Any, Callable
7+
from typing import Dict, Union, Optional, cast, Any, Callable, Type
88
from datetime import datetime
99
import threading
1010
from abc import ABC, abstractmethod
@@ -52,12 +52,14 @@ class AbstractSyncWebSocketClient(ABC): # pylint: disable=too-many-instance-att
5252
_listen_thread: Union[threading.Thread, None]
5353
_delegate: Optional[Speaker] = None
5454

55+
_thread_cls: Type[threading.Thread]
56+
5557
_kwargs: Optional[Dict] = None
5658
_addons: Optional[Dict] = None
5759
_options: Optional[Dict] = None
5860
_headers: Optional[Dict] = None
5961

60-
def __init__(self, config: DeepgramClientOptions, endpoint: str = ""):
62+
def __init__(self, config: DeepgramClientOptions, endpoint: str = "", thread_cls: Type[threading.Thread] = threading.Thread) -> None:
6163
if config is None:
6264
raise DeepgramError("Config is required")
6365
if endpoint == "":
@@ -73,6 +75,8 @@ def __init__(self, config: DeepgramClientOptions, endpoint: str = ""):
7375

7476
self._listen_thread = None
7577

78+
self._thread_cls = thread_cls
79+
7680
# exit
7781
self._exit_event = threading.Event()
7882

@@ -152,7 +156,7 @@ def start(
152156
self._delegate.set_push_callback(self._process_message)
153157
else:
154158
self._logger.notice("create _listening thread")
155-
self._listen_thread = threading.Thread(target=self._listening)
159+
self._listen_thread = self._thread_cls(target=self._listening)
156160
self._listen_thread.start()
157161

158162
# debug the threads

deepgram/clients/listen/v1/websocket/client.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55
import time
66
import logging
7-
from typing import Dict, Union, Optional, cast, Any, Callable
7+
from typing import Dict, Union, Optional, cast, Any, Callable, Type
88
from datetime import datetime
99
import threading
1010

@@ -55,12 +55,14 @@ class ListenWebSocketClient(
5555
_flush_thread: Union[threading.Thread, None]
5656
_last_datagram: Optional[datetime] = None
5757

58+
_thread_cls: Type[threading.Thread]
59+
5860
_kwargs: Optional[Dict] = None
5961
_addons: Optional[Dict] = None
6062
_options: Optional[Dict] = None
6163
_headers: Optional[Dict] = None
6264

63-
def __init__(self, config: DeepgramClientOptions):
65+
def __init__(self, config: DeepgramClientOptions, thread_cls: Type[threading.Thread] = threading.Thread):
6466
if config is None:
6567
raise DeepgramError("Config is required")
6668

@@ -78,6 +80,8 @@ def __init__(self, config: DeepgramClientOptions):
7880
self._last_datagram = None
7981
self._lock_flush = threading.Lock()
8082

83+
self._thread_cls = thread_cls
84+
8185
# init handlers
8286
self._event_handlers = {
8387
event: [] for event in LiveTranscriptionEvents.__members__.values()
@@ -154,15 +158,15 @@ def start(
154158
# keepalive thread
155159
if self._config.is_keep_alive_enabled():
156160
self._logger.notice("keepalive is enabled")
157-
self._keep_alive_thread = threading.Thread(target=self._keep_alive)
161+
self._keep_alive_thread = self._thread_cls(target=self._keep_alive)
158162
self._keep_alive_thread.start()
159163
else:
160164
self._logger.notice("keepalive is disabled")
161165

162166
# flush thread
163167
if self._config.is_auto_flush_reply_enabled():
164168
self._logger.notice("autoflush is enabled")
165-
self._flush_thread = threading.Thread(target=self._flush)
169+
self._flush_thread = self._thread_cls(target=self._flush)
166170
self._flush_thread.start()
167171
else:
168172
self._logger.notice("autoflush is disabled")

0 commit comments

Comments
 (0)