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
@@ -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