fix multiprocessing support on windows

This commit is contained in:
dj2ls 2022-02-17 14:25:22 +01:00
parent a8d1d576ad
commit 406518503a
4 changed files with 27 additions and 17 deletions

View file

@ -1,6 +1,7 @@
import json
import sys
import multiprocessing
####################################################
# https://stackoverflow.com/questions/7088672/pyaudio-working-but-spits-out-error-messages-each-time
# https://github.com/DJ2LS/FreeDATA/issues/22
@ -10,8 +11,8 @@ import json
from ctypes import *
from contextlib import contextmanager
import pyaudio
from multiprocessing import Process, Queue
AUDIO_DEVICES = Queue()
AUDIO_DEVICES = multiprocessing.Queue()
@ -31,13 +32,15 @@ def noalsaerr():
# with noalsaerr():
# p = pyaudio.PyAudio()
######################################################
#####################################################
def get_audio_devices():
p = Process(target=update_audio_devices)
p = multiprocessing.Process(target=update_audio_devices)
p.start()
p.join()
audio_devices = AUDIO_DEVICES.get()
return audio_devices
def update_audio_devices():
@ -71,6 +74,7 @@ def update_audio_devices():
output_devices.append({"id": i, "name": str(name)})
p.terminate()
AUDIO_DEVICES.put([input_devices, output_devices])
return [input_devices, output_devices]

View file

@ -28,6 +28,7 @@ import audio
import sock
import atexit
import signal
import multiprocessing
# signal handler for closing aplication
def signal_handler(sig, frame):
@ -58,12 +59,11 @@ class DAEMON():
def update_audio_devices(self):
while 1:
#print("update audio")
try:
static.AUDIO_INPUT_DEVICES, static.AUDIO_OUTPUT_DEVICES = audio.get_audio_devices()
# WE NEED TO WAIT SOME MORE TIME, SO WE ARE NOT CAUSING A CRASH ON RASPBERRY PIs!
# pyAudio will crash there if we are opening and closing pyaudio too fast
time.sleep(3)
if not static.TNCSTARTED:
print("updating audio devices")
static.AUDIO_INPUT_DEVICES, static.AUDIO_OUTPUT_DEVICES = audio.get_audio_devices()
time.sleep(2)
except Exception as e:
print(e)
@ -281,10 +281,13 @@ class DAEMON():
if __name__ == '__main__':
# we need to run this on windows for multiprocessing support
multiprocessing.freeze_support()
# --------------------------------------------GET PARAMETER INPUTS
PARSER = argparse.ArgumentParser(description='Simons TEST TNC')
PARSER.add_argument('--port', dest="socket_port",default=3001, help="Socket port", type=int)
#PARSER.add_argument('--multiprocessing-fork', dest="multi_processing", help="test", type=str)
#PARSER.add_argument('pipe_handle', dest="pipe_handle_param", help="test", type=str)
ARGS = PARSER.parse_args()
static.DAEMONPORT = ARGS.socket_port
@ -308,4 +311,4 @@ if __name__ == '__main__':
while True:
time.sleep(1)
time.sleep(1)

View file

@ -20,6 +20,7 @@ import modem
import sys
import signal
import time
import multiprocessing
# signal handler for closing aplication
@ -31,7 +32,8 @@ def signal_handler(sig, frame):
signal.signal(signal.SIGINT, signal_handler)
if __name__ == '__main__':
# we need to run this on windows for multiprocessing support
multiprocessing.freeze_support()
# --------------------------------------------GET PARAMETER INPUTS
PARSER = argparse.ArgumentParser(description='FreeDATA TNC')
PARSER.add_argument('--mycall', dest="mycall", default="AA0AA", help="My callsign", type=str)
@ -113,4 +115,4 @@ if __name__ == '__main__':
structlog.get_logger("structlog").error("[TNC] Starting TCP/IP socket failed", port=static.PORT, e=e)
while 1:
time.sleep(1)
time.sleep(1)

View file

@ -24,7 +24,7 @@ import re
import queue
import codec2
import audio
import multiprocessing
@ -166,7 +166,8 @@ class RF():
try:
structlog.get_logger("structlog").debug("[TNC] starting pyaudio callback")
self.audio_stream.start_stream()
#self.audio_stream.start_stream()
multiprocessing.Process(target=self.audio_stream.start_stream).start()
except Exception as e:
structlog.get_logger("structlog").error("[TNC] starting pyaudio callback failed", e=e)