So, I have a socket.io connection with a React.js frontend that sends a blob of wav audio data every 100 ms to my Flask backend. On my backend, I am maintaining a thread safe queue
audio_buffer = queue.Queue()
@socketio.on('stream')
def stream(data):
#Data is a properly formatted wav file in bytes
audio_buffer.put(data)
Now, my goal is that while my backend is taking the streamed data from the frontend, I also want to send the data to google ASR from my backend at the same time. I am basing my code here on the documentation here: https://cloud.google.com/speech-to-text/docs/streaming-recognize
Basically, I am going to create a generator with a reference to the queue and give that to the google api.
audio_generator = stream.generator() #Here, I wrote a new method for creating a generator from audio_buffer
requests = (
speech.StreamingRecognizeRequest(audio_content=content)
for content in audio_generator
)
responses = client.streaming_recognize(streaming_config, requests)
# Now, put the transcription responses to use.
listen_print_loop(responses)
Now the issue that I have is that essentially, if I try to send things to the google api, my backend stops listening to the frontend and just waits for the queue to be populated, ignoring socket.io requests from the client. For example, I tried to have the client to send a start command for the backend to create the generator and start streaming data to google while the client also sends data to populate the queue through the stream method. What happened was that it got stuck on trying to stream data to google and never got around to actually listening to the data blobs.
My question is that should I attempt to use threading or celery to try and run the google api code simultaneously? Basically, I am thinking of instantiating a thread or worker to do this google api streaming recognize, and the main flask thread would keep listening to the client and populate the queue. Is this a sound approach? Is there a library that implements this for me?
Update 5/16: So I am attempting to run the google api on a standard thread. My code is pretty simple. On start, I start a thread with
yourThread = threading.Thread(target=recognize, args=[stream])
and my recognize code passes in the thread safe queue and actually sends the code to the google api properly. However, my issue now is that emits that I put in the code don't actually make it to the client. My code is based on the google api code:
for response in responses:
#Some other code from the tutorial
socketio.emit("transcription", transcript)
that last emit call never actually gets to the frontend at all. Now, looking around, I found the same issue here: https://github.com/miguelgrinberg/Flask-SocketIO/issues/1239
import httplib2shim
httplib2shim.patch()
but when I tried the fix over there, and switched async_mode to eventlet, the google api calls still worked, but none of the emits still get to the client