Skip to content
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

Async server to handle multiple requests #109

Merged
merged 3 commits into from
Jul 18, 2024
Merged

Async server to handle multiple requests #109

merged 3 commits into from
Jul 18, 2024

Conversation

criminact
Copy link
Contributor

@criminact criminact commented Jul 18, 2024

This Server Handles Concurrency and it capable to serve multiple clients in parallel

This Server Handles Concurrency and it capable to server multiple clients as once
@KoljaB
Copy link
Owner

KoljaB commented Jul 18, 2024

Thanks a lot for putting in the work. Will test that soon, there are some issues that I see already:

  1. speaking
    def play_text_to_speech(self, text):
        self.speaking = True
        try:
            self.stream.feed(text)
            logging.debug(f"Playing audio for text: {text}")
            print(f"Synthesizing: \"{text}\"")
            self.stream.play_async(on_audio_chunk=self.on_audio_chunk, muted=True)
        finally:
            self.speaking = False

self.speaking will instantly be False again, because feed and play_async are both nonblocking.

  1. handling of request_handler.speaking
@app.get("/tts")
def tts(request: Request, text: str = Query(...)):
    with tts_lock:
        request_handler = TTSRequestHandler(current_engine)
        browser_request = is_browser_request(request)

        if play_text_to_speech_semaphore.acquire(blocking=False):
            try:
                if not request_handler.speaking:
                    threading.Thread(
                        target=request_handler.play_text_to_speech,
                        args=(text,),
                        daemon=True).start()
            finally:
                play_text_to_speech_semaphore.release()

        return StreamingResponse(
            request_handler.audio_chunk_generator(browser_request),
            media_type="audio/wav"
        )

Even if request_handler.speaking could be True - so in the case we are already speaking, we don't start a play thread and just do nothing here?

  1. duplicate routes
    @app.get("/tts-text") route contains calls to play_text_to_speech and audio_chunk_generator and is not doing much more than @app.get("/tts").

Thx again, will test as soon as I find time...

1. Removed tts-text (duplicate endpoint)
2. handler request_handler.speaking = False on on_audio_stream_stop
3. Responding back with 500 code and Error Message when request_handler is already busy speaking/playing.
@criminact
Copy link
Contributor Author

criminact commented Jul 18, 2024

@KoljaB

Here is what I have updated -

Pt1.
I have removed try block and kept self.speaking = False in

def on_audio_stream_stop(self):
        self.audio_queue.put(None)
        self.speaking = False

Pt2.
I have removed request_handler.speaking if else statement, since it should never go in else block anyway?

if play_text_to_speech_semaphore.acquire(blocking=False):
      try:
          threading.Thread(
              target=request_handler.play_text_to_speech,
              args=(text,),
              daemon=True).start()
      finally:
          play_text_to_speech_semaphore.release()

Pt3.
Removed the duplicate code

@KoljaB
Copy link
Owner

KoljaB commented Jul 18, 2024

Thanks a lot, now it looks good!

@KoljaB KoljaB merged commit 2fc4eac into KoljaB:master Jul 18, 2024
@KoljaB
Copy link
Owner

KoljaB commented Jul 18, 2024

Moved the async_server.py file to the example_fast_api folder.

@criminact
Copy link
Contributor Author

Thanks. I was about to 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants