How can I correctly make my code run faster? Following is my attempt at making use of multithreading. However, I do not understand where to join the threads. I want the results to be printed serially, but computed parallely.
import queue
import datetime
from time import sleep
import sounddevice as sd
import threading
def transcribe(audio):
sleep(10)
print("Hello World")
class RealtimeTranscriber():
NUM_BLOCK = 20
N_FRAMES = 3000
SAMPLE_RATE = 16000
def __init__(self):
def on_parse_block(indata, frame_count, time, status):
del frame_count, time, status
self.q.put((datetime.datetime.now(), indata.ravel()))
def on_parse_sequence():
pass
self.q = queue.Queue()
self.stream = sd.InputStream(
device=0,
channels=1,
samplerate=self.SAMPLE_RATE,
blocksize=self.N_FRAMES * self.NUM_BLOCK,
dtype="float32",
callback=on_parse_block,
finished_callback=on_parse_sequence
)
with self.stream:
while True:
try:
try:
timestamp, audio = self.q.get()
t = threading.Thread(target=self.transcribe, args=(audio,))
t.start()
except queue.Empty:
pass
except KeyboardInterrupt:
break
if __name__ == "__main__":
transcriber = RealtimeTranscriber()