44import json
55import time
66import logging
7- from typing import Dict , Union , Optional , cast , Any , Callable
7+ from typing import Dict , Union , Optional , cast , Any , Callable , Type
88from datetime import datetime
99import threading
1010
@@ -38,10 +38,12 @@ class ListenWebSocketClient(
3838 """
3939 Client for interacting with Deepgram's live transcription services over WebSockets.
4040
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.
4242
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.
4547 """
4648
4749 _logger : verboselogs .VerboseLogger
@@ -55,12 +57,18 @@ class ListenWebSocketClient(
5557 _flush_thread : Union [threading .Thread , None ]
5658 _last_datagram : Optional [datetime ] = None
5759
60+ _thread_cls : Type [threading .Thread ]
61+
5862 _kwargs : Optional [Dict ] = None
5963 _addons : Optional [Dict ] = None
6064 _options : Optional [Dict ] = None
6165 _headers : Optional [Dict ] = None
6266
63- def __init__ (self , config : DeepgramClientOptions ):
67+ def __init__ (
68+ self ,
69+ config : DeepgramClientOptions ,
70+ thread_cls : Type [threading .Thread ] = threading .Thread ,
71+ ):
6472 if config is None :
6573 raise DeepgramError ("Config is required" )
6674
@@ -78,13 +86,19 @@ def __init__(self, config: DeepgramClientOptions):
7886 self ._last_datagram = None
7987 self ._lock_flush = threading .Lock ()
8088
89+ self ._thread_cls = thread_cls
90+
8191 # init handlers
8292 self ._event_handlers = {
8393 event : [] for event in LiveTranscriptionEvents .__members__ .values ()
8494 }
8595
8696 # 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+ )
88102
89103 # pylint: disable=too-many-statements,too-many-branches
90104 def start (
@@ -154,15 +168,15 @@ def start(
154168 # keepalive thread
155169 if self ._config .is_keep_alive_enabled ():
156170 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 )
158172 self ._keep_alive_thread .start ()
159173 else :
160174 self ._logger .notice ("keepalive is disabled" )
161175
162176 # flush thread
163177 if self ._config .is_auto_flush_reply_enabled ():
164178 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 )
166180 self ._flush_thread .start ()
167181 else :
168182 self ._logger .notice ("autoflush is disabled" )
0 commit comments