4
4
import json
5
5
import time
6
6
import logging
7
- from typing import Dict , Union , Optional , cast , Any , Callable
7
+ from typing import Dict , Union , Optional , cast , Any , Callable , Type
8
8
from datetime import datetime
9
9
import threading
10
10
@@ -38,10 +38,12 @@ class ListenWebSocketClient(
38
38
"""
39
39
Client for interacting with Deepgram's live transcription services over WebSockets.
40
40
41
- This class provides methods to establish a WebSocket connection for live transcription and handle real-time transcription events.
41
+ This class provides methods to establish a WebSocket connection for live transcription and handle real-time transcription events.
42
42
43
- Args:
44
- config (DeepgramClientOptions): all the options for the client.
43
+ Args:
44
+ config (DeepgramClientOptions): all the options for the client.
45
+ thread_cls (Type[threading.Thread]): optional thread class to use for creating threads,
46
+ defaults to threading.Thread. Useful for custom thread management like ContextVar support.
45
47
"""
46
48
47
49
_logger : verboselogs .VerboseLogger
@@ -55,12 +57,18 @@ class ListenWebSocketClient(
55
57
_flush_thread : Union [threading .Thread , None ]
56
58
_last_datagram : Optional [datetime ] = None
57
59
60
+ _thread_cls : Type [threading .Thread ]
61
+
58
62
_kwargs : Optional [Dict ] = None
59
63
_addons : Optional [Dict ] = None
60
64
_options : Optional [Dict ] = None
61
65
_headers : Optional [Dict ] = None
62
66
63
- def __init__ (self , config : DeepgramClientOptions ):
67
+ def __init__ (
68
+ self ,
69
+ config : DeepgramClientOptions ,
70
+ thread_cls : Type [threading .Thread ] = threading .Thread ,
71
+ ):
64
72
if config is None :
65
73
raise DeepgramError ("Config is required" )
66
74
@@ -78,13 +86,19 @@ def __init__(self, config: DeepgramClientOptions):
78
86
self ._last_datagram = None
79
87
self ._lock_flush = threading .Lock ()
80
88
89
+ self ._thread_cls = thread_cls
90
+
81
91
# init handlers
82
92
self ._event_handlers = {
83
93
event : [] for event in LiveTranscriptionEvents .__members__ .values ()
84
94
}
85
95
86
96
# call the parent constructor
87
- super ().__init__ (self ._config , self ._endpoint )
97
+ super ().__init__ (
98
+ config = self ._config ,
99
+ endpoint = self ._endpoint ,
100
+ thread_cls = self ._thread_cls ,
101
+ )
88
102
89
103
# pylint: disable=too-many-statements,too-many-branches
90
104
def start (
@@ -154,15 +168,15 @@ def start(
154
168
# keepalive thread
155
169
if self ._config .is_keep_alive_enabled ():
156
170
self ._logger .notice ("keepalive is enabled" )
157
- self ._keep_alive_thread = threading . Thread (target = self ._keep_alive )
171
+ self ._keep_alive_thread = self . _thread_cls (target = self ._keep_alive )
158
172
self ._keep_alive_thread .start ()
159
173
else :
160
174
self ._logger .notice ("keepalive is disabled" )
161
175
162
176
# flush thread
163
177
if self ._config .is_auto_flush_reply_enabled ():
164
178
self ._logger .notice ("autoflush is enabled" )
165
- self ._flush_thread = threading . Thread (target = self ._flush )
179
+ self ._flush_thread = self . _thread_cls (target = self ._flush )
166
180
self ._flush_thread .start ()
167
181
else :
168
182
self ._logger .notice ("autoflush is disabled" )
0 commit comments