5
5
import json
6
6
import logging
7
7
from typing import Dict , Union , Optional , cast , Any
8
+ import threading
8
9
9
10
import websockets
10
11
from websockets .client import WebSocketClientProtocol
@@ -49,8 +50,8 @@ class AsyncLiveClient: # pylint: disable=too-many-instance-attributes
49
50
50
51
_socket : WebSocketClientProtocol
51
52
_event_handlers : Dict [LiveTranscriptionEvents , list ]
52
- _listen_thread : asyncio .Task
53
- _keep_alive_thread : asyncio .Task
53
+ _listen_thread : Union [ asyncio .Task , None ]
54
+ _keep_alive_thread : Union [ asyncio .Task , None ]
54
55
55
56
_kwargs : Optional [Dict ] = None
56
57
_addons : Optional [Dict ] = None
@@ -142,6 +143,11 @@ async def start(
142
143
)
143
144
self ._exit_event .clear ()
144
145
146
+ # debug the threads
147
+ for thread in threading .enumerate ():
148
+ self ._logger .debug ("after running thread: %s" , thread .name )
149
+ self ._logger .debug ("number of active threads: %s" , threading .active_count ())
150
+
145
151
# listen thread
146
152
self ._listen_thread = asyncio .create_task (self ._listening ())
147
153
@@ -152,6 +158,11 @@ async def start(
152
158
else :
153
159
self ._logger .notice ("keepalive is disabled" )
154
160
161
+ # debug the threads
162
+ for thread in threading .enumerate ():
163
+ self ._logger .debug ("after running thread: %s" , thread .name )
164
+ self ._logger .debug ("number of active threads: %s" , threading .active_count ())
165
+
155
166
# push open event
156
167
await self ._emit (
157
168
LiveTranscriptionEvents (LiveTranscriptionEvents .Open ),
@@ -195,11 +206,28 @@ async def _emit(self, event: LiveTranscriptionEvents, *args, **kwargs) -> None:
195
206
"""
196
207
Emits events to the registered event handlers.
197
208
"""
209
+
210
+ self ._logger .debug ("AsyncLiveClient._emit ENTER" )
211
+
212
+ # debug the threads
213
+ for thread in threading .enumerate ():
214
+ self ._logger .debug ("after running thread: %s" , thread .name )
215
+ self ._logger .debug ("number of active threads: %s" , threading .active_count ())
216
+
217
+ tasks = []
198
218
for handler in self ._event_handlers [event ]:
199
- if asyncio .iscoroutinefunction (handler ):
200
- await handler (self , * args , ** kwargs )
201
- else :
202
- asyncio .create_task (handler (self , * args , ** kwargs ))
219
+ tasks .append (asyncio .create_task (handler (self , * args , ** kwargs )))
220
+
221
+ if len (tasks ) > 0 :
222
+ self ._logger .debug ("waiting for tasks to finish..." )
223
+ await asyncio .gather (* filter (None , tasks ), return_exceptions = True )
224
+
225
+ # debug the threads
226
+ for thread in threading .enumerate ():
227
+ self ._logger .debug ("after running thread: %s" , thread .name )
228
+ self ._logger .debug ("number of active threads: %s" , threading .active_count ())
229
+
230
+ self ._logger .debug ("AsyncLiveClient._emit LEAVE" )
203
231
204
232
# pylint: disable=too-many-return-statements,too-many-statements,too-many-locals
205
233
async def _listening (self ) -> None :
@@ -608,11 +636,15 @@ async def finish(self) -> bool:
608
636
self ._logger .verbose ("cancelling tasks..." )
609
637
try :
610
638
# Before cancelling, check if the tasks were created
639
+ # debug the threads
640
+ for thread in threading .enumerate ():
641
+ self ._logger .debug ("before running thread: %s" , thread .name )
642
+ self ._logger .debug ("number of active threads: %s" , threading .active_count ())
643
+
611
644
tasks = []
612
- if self ._config .options .get ("keepalive" ) == "true" :
613
- if self ._keep_alive_thread is not None :
614
- self ._keep_alive_thread .cancel ()
615
- tasks .append (self ._keep_alive_thread )
645
+ if self ._keep_alive_thread is not None :
646
+ self ._keep_alive_thread .cancel ()
647
+ tasks .append (self ._keep_alive_thread )
616
648
if self ._listen_thread is not None :
617
649
self ._listen_thread .cancel ()
618
650
tasks .append (self ._listen_thread )
@@ -621,6 +653,11 @@ async def finish(self) -> bool:
621
653
await asyncio .gather (* filter (None , tasks ), return_exceptions = True )
622
654
self ._logger .notice ("threads joined" )
623
655
656
+ # debug the threads
657
+ for thread in threading .enumerate ():
658
+ self ._logger .debug ("after running thread: %s" , thread .name )
659
+ self ._logger .debug ("number of active threads: %s" , threading .active_count ())
660
+
624
661
self ._logger .notice ("finish succeeded" )
625
662
self ._logger .spam ("AsyncLiveClient.finish LEAVE" )
626
663
return True
0 commit comments