possible fix for #132

it seems, pyaudio isnt terminated correctly. running it within a subprocess which will be closed after execution, pyaudio seems to be terminated and not causing a problem anymore on raspberry pi. Yay
This commit is contained in:
dj2ls 2022-02-17 10:11:12 +01:00
parent 7a93f0a824
commit efc700b1f7
2 changed files with 33 additions and 17 deletions

View file

@ -10,6 +10,10 @@ import json
from ctypes import * from ctypes import *
from contextlib import contextmanager from contextlib import contextmanager
import pyaudio import pyaudio
from multiprocessing import Process, Queue
AUDIO_DEVICES = Queue()
ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p) ERROR_HANDLER_FUNC = CFUNCTYPE(None, c_char_p, c_int, c_char_p, c_int, c_char_p)
@ -29,8 +33,13 @@ def noalsaerr():
# p = pyaudio.PyAudio() # p = pyaudio.PyAudio()
###################################################### ######################################################
def get_audio_devices(): def get_audio_devices():
p = Process(target=update_audio_devices)
p.start()
p.join()
return AUDIO_DEVICES.get()
def update_audio_devices():
# UPDATE LIST OF AUDIO DEVICES # UPDATE LIST OF AUDIO DEVICES
try: try:
# we need to "try" this, because sometimes libasound.so isn't in the default place # we need to "try" this, because sometimes libasound.so isn't in the default place
@ -61,5 +70,6 @@ def get_audio_devices():
output_devices.append({"id": i, "name": str(name)}) output_devices.append({"id": i, "name": str(name)})
p.terminate() p.terminate()
AUDIO_DEVICES.put([input_devices, output_devices])
return [input_devices, output_devices]

View file

@ -78,17 +78,15 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
data = SOCKET_QUEUE.get() data = SOCKET_QUEUE.get()
sock_data = bytes(data, 'utf-8') sock_data = bytes(data, 'utf-8')
sock_data += b'\n' # append line limiter sock_data += b'\n' # append line limiter
# send data to all clients # send data to all clients
try: #try:
for client in CONNECTED_CLIENTS: for client in CONNECTED_CLIENTS:
try: try:
client.send(sock_data) client.send(sock_data)
except: except:
print("connection lost...") print("connection lost...")
CONNECTED_CLIENTS.remove(self.request)
except:
print("client not anymore in client list")
# we want to transmit scatter data only once to reduce network traffic # we want to transmit scatter data only once to reduce network traffic
static.SCATTER = [] static.SCATTER = []
# we want to display INFO messages only once # we want to display INFO messages only once
@ -106,8 +104,8 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
if chunk == b'': if chunk == b'':
#print("connection broken. Closing...") #print("connection broken. Closing...")
self.connection_alive = False self.connection_alive = False
print(chunk)
if data.startswith(b'{') and data.endswith(b'}\n'): if data.startswith(b'{') and data.endswith(b'}\n'):
# split data by \n if we have multiple commands in socket buffer # split data by \n if we have multiple commands in socket buffer
data = data.split(b'\n') data = data.split(b'\n')
# remove empty data # remove empty data
@ -119,7 +117,15 @@ class ThreadedTCPRequestHandler(socketserver.StreamRequestHandler):
process_tnc_commands(commands) process_tnc_commands(commands)
else: else:
process_daemon_commands(commands) process_daemon_commands(commands)
# wait some time between processing multiple commands
# this is only a first test to avoid doubled transmission
# we might improve this by only processing one command or
# doing some kind of selection to determin which commands need to be dropped
# and which one can be processed during a running transmission
time.sleep(3)
# finally delete our rx buffer to be ready for new commands # finally delete our rx buffer to be ready for new commands
data = bytes() data = bytes()
except Exception as e: except Exception as e:
@ -438,4 +444,4 @@ def send_daemon_state():
return jsondata return jsondata
except Exception as e: except Exception as e:
print(e) print(e)
return None return None