Threaded and Async IO Task Support

Threaded and Async IO Task Support

Our Python SDK supports the Threaded and Async IO Task API models, allowing developers to build efficient, non-blocking applications.

Threaded API

The SDK also supports a Threading API via its classes/functions, allowing concurrent task execution. This is particularly useful for CPU and IO-bound operations when performing background operations without blocking the main (or other) thread(s). Utilizing Python’s threading module, you can create and manage multiple threads, ensuring your application remains efficient and responsive.

These Threaded components will work with asynchronous code that utilizes the async/await classes and functions. Even if your code uses Async IO classes/functions, you can still use these Threaded API classes/functions.

The best use cases for using Threaded are:

  • Heavily Parallelized Applications, such as backend applications behind an API (ie a REST API service)
  • CPU-bound Operations, such as ML applications
  • IO-bound Operations, such as ML applications or Backup solutions

Pushing a lot of audio data (ie IO operations) over the internet, like performing live transcription of real-time audio, qualifies for using the Threaded API over Async IO Task. In other words, for the overwhleming majority of cases, you should be using this Threaded API over Async IO Tasks for real-time transcription.

Async IO Task API

Async IO Tasks can handle multiple tasks concurrently, making it ideal for high-level structured network code or when handling multiple I/O-bound tasks simultaneously. This is ideal for web-based applications that interact with a web browser. This functionality leverages Python’s asyncio library, enabling you to write asynchronous code using async and await keywords, ensuring smoother and more responsive applications.

The best use cases for using Async IO Tasks are:

  • Web-based Browser Applications
  • Non-Real-Time based Applications, see the previous item
    • Over simplifying this, since an Async IO “Thread” or Task is run exclusively from other threads/tasks (ie other tasks are suspended), operations on other threads no longer are “real-time”. Please see asyncio documentation.
  • Willing to trade off performance for ease of programming

Accessing the API Types

Threaded API Classes

You can find the Threaded API classes through the convenient dot notation accessors below or by directly creating the [Client] contained brackets:

  • Real-Time/Live Transcription: deepgram.listen.v1.connect() for WebSocket connections
  • Text-to-Speech REST: deepgram.speak.v1.audio.generate() for REST API
  • Text-to-Speech WebSocket: deepgram.speak.v1.connect() for streaming TTS
  • Pre-Recorded Transcription: deepgram.listen.v1.media.transcribe_url() or deepgram.listen.v1.media.transcribe_file()
  • Text Intelligence: deepgram.read.v1.text.analyze() for text analysis
  • Management: Use the management API directly through the client

Async IO Task Classes

You can find the Async IO Task API (async/await) classes through the convenient dot notation accessors below or by directly creating the [Client] contained brackets:

  • Real-Time/Live Transcription: Use async with deepgram.listen.v1.connect() for async WebSocket connections
  • Text-to-Speech REST: Use await deepgram.speak.v1.audio.generate() for async REST API
  • Text-to-Speech WebSocket: Use async with deepgram.speak.v1.connect() for async streaming TTS
  • Pre-Recorded Transcription: Use await deepgram.listen.v1.media.transcribe_url() or await deepgram.listen.v1.media.transcribe_file()
  • Text Intelligence: Use await deepgram.read.v1.text.analyze() for async text analysis
  • Management: Use async management API calls directly through the client

Examples

The GitHub repo contains examples for the Threaded and Async IO Task APIs for the 5 majority category of APIs: