I am working on a realtime application. The application takes realtime audio, send to a recogniser, and get results back. I am using a socket for both sending and receiving. For debugging, I use a wave file. So I read audio, I would like to send the audio as if it is being played in realtime. I use time.sleep trying to synchronise the send, it caused the send to be slower than realtime. Should I use two threads, one for send and one for receive?
while True:
readable, writable, exceptional = select.select([s], [s], [])
if readable:
try:
data = s.recv(1024)
except:
break
if data == "":
break
else:
for line in data.split("\n"):
if not line: continue
t, w = line.split()
if w == "<EOL>":
w = ""
addResult(t, w)
if writable:
now = time.time()
if (now - start_time) / 0.01 >= frame_count - 5:
frame_count += 1
data = wav.readframes(80)
if data is None:
s.shutdown(s.SHUT_WR)
continue
s.send(struct.pack('I', len(data)) + data)
else:
time.sleep(0.005)
if exceptional:
print "Hangup"
break
recvit is not given, that only whole lines are read, therefore yourdata-string could end between a "<EO" and the "L>" is in the nextrecv.now - start_timeand the number of frames sent.