Skip to content

Wait for All Callbacks in _emit to Finish, Add More Thread Tracing #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 41 additions & 4 deletions deepgram/clients/live/v1/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import logging
from typing import Dict, Union, Optional, cast, Any
from datetime import datetime
import threading

import websockets
from websockets.client import WebSocketClientProtocol
Expand Down Expand Up @@ -157,6 +158,11 @@ async def start(
)
self._exit_event.clear()

# debug the threads
for thread in threading.enumerate():
self._logger.debug("after running thread: %s", thread.name)
self._logger.debug("number of active threads: %s", threading.active_count())

# listen thread
self._listen_thread = asyncio.create_task(self._listening())

Expand All @@ -174,6 +180,11 @@ async def start(
else:
self._logger.notice("autoflush is disabled")

# debug the threads
for thread in threading.enumerate():
self._logger.debug("after running thread: %s", thread.name)
self._logger.debug("number of active threads: %s", threading.active_count())

# push open event
await self._emit(
LiveTranscriptionEvents(LiveTranscriptionEvents.Open),
Expand Down Expand Up @@ -217,12 +228,28 @@ async def _emit(self, event: LiveTranscriptionEvents, *args, **kwargs) -> None:
"""
Emits events to the registered event handlers.
"""
self._logger.debug("AsyncLiveClient._emit ENTER")
self._logger.debug("callback handlers for: %s", event)

# debug the threads
for thread in threading.enumerate():
self._logger.debug("after running thread: %s", thread.name)
self._logger.debug("number of active threads: %s", threading.active_count())

tasks = []
for handler in self._event_handlers[event]:
if asyncio.iscoroutinefunction(handler):
await handler(self, *args, **kwargs)
else:
asyncio.create_task(handler(self, *args, **kwargs))
tasks.append(asyncio.create_task(handler(self, *args, **kwargs)))

if len(tasks) > 0:
self._logger.debug("waiting for tasks to finish...")
await asyncio.gather(*filter(None, tasks), return_exceptions=True)

# debug the threads
for thread in threading.enumerate():
self._logger.debug("after running thread: %s", thread.name)
self._logger.debug("number of active threads: %s", threading.active_count())

self._logger.debug("AsyncLiveClient._emit LEAVE")

# pylint: disable=too-many-return-statements,too-many-statements,too-many-locals,too-many-branches
async def _listening(self) -> None:
Expand Down Expand Up @@ -796,6 +823,11 @@ async def finish(self) -> bool:
self._logger.verbose("cancelling tasks...")
try:
# Before cancelling, check if the tasks were created
# debug the threads
for thread in threading.enumerate():
self._logger.debug("before running thread: %s", thread.name)
self._logger.debug("number of active threads: %s", threading.active_count())

tasks = []
if self._keep_alive_thread is not None:
self._keep_alive_thread.cancel()
Expand All @@ -816,6 +848,11 @@ async def finish(self) -> bool:
await asyncio.gather(*filter(None, tasks), return_exceptions=True)
self._logger.notice("threads joined")

# debug the threads
for thread in threading.enumerate():
self._logger.debug("after running thread: %s", thread.name)
self._logger.debug("number of active threads: %s", threading.active_count())

self._logger.notice("finish succeeded")
self._logger.spam("AsyncLiveClient.finish LEAVE")
return True
Expand Down
22 changes: 21 additions & 1 deletion deepgram/clients/live/v1/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
# Use of this source code is governed by a MIT license that can be found in the LICENSE file.
# SPDX-License-Identifier: MIT
import json
import threading
import time
import logging
from typing import Dict, Union, Optional, cast, Any
from datetime import datetime
import threading

from websockets.sync.client import connect, ClientConnection
import websockets
Expand Down Expand Up @@ -160,6 +160,11 @@ def start(
self._socket = connect(url_with_params, additional_headers=combined_headers)
self._exit_event.clear()

# debug the threads
for thread in threading.enumerate():
self._logger.debug("after running thread: %s", thread.name)
self._logger.debug("number of active threads: %s", threading.active_count())

# listening thread
self._listen_thread = threading.Thread(target=self._listening)
self._listen_thread.start()
Expand All @@ -180,6 +185,11 @@ def start(
else:
self._logger.notice("autoflush is disabled")

# debug the threads
for thread in threading.enumerate():
self._logger.debug("after running thread: %s", thread.name)
self._logger.debug("number of active threads: %s", threading.active_count())

# push open event
self._emit(
LiveTranscriptionEvents(LiveTranscriptionEvents.Open),
Expand Down Expand Up @@ -780,6 +790,11 @@ def finish(self) -> bool:
"""
self._logger.spam("LiveClient.finish ENTER")

# debug the threads
for thread in threading.enumerate():
self._logger.debug("before running thread: %s", thread.name)
self._logger.debug("number of active threads: %s", threading.active_count())

# signal exit
self._signal_exit()

Expand All @@ -800,6 +815,11 @@ def finish(self) -> bool:
self._listen_thread = None
self._logger.notice("listening thread joined")

# debug the threads
for thread in threading.enumerate():
self._logger.debug("before running thread: %s", thread.name)
self._logger.debug("number of active threads: %s", threading.active_count())

self._logger.notice("finish succeeded")
self._logger.spam("LiveClient.finish LEAVE")
return True
Expand Down