Starting a subprocess via python multiprocessing hangs
I'm using pyAudio to listen to the audio device and do some "stuff" in the background while the main program continues to run.
I started out with a second script, but would like to consolidate into a single script for supportability. When I moved the functions in and use Process to start up the listener it simply hangs and never runs.
Here's the simplified snippets of code:
def listener(self, q):
CHANNELS = 2
RATE = 44100
INPUT_BLOCK_TIME = 0.05
FORMAT = pyaudio.paInt16
RATE = 44100
INPUT_FRAMES_PER_BLOCK = int(RATE*INPUT_BLOCK_TIME)
p = pyaudio.PyAudio()
stream = p.open(format = FORMAT,
channels = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = INPUT_FRAMES_PER_BLOCK)
q.put(os.getpid())
import time
time.sleep(300)
def startListener(self):
q = Queue()
p = Process(target=self.listener, args=[q])
p.daemon=True
p.start()
print q.get()
Now if I remove the following stream setup then I get the process ID back as expected:
stream = p.open(format = FORMAT,
channe开发者_C百科ls = CHANNELS,
rate = RATE,
input = True,
frames_per_buffer = INPUT_FRAMES_PER_BLOCK)
Is there something about multiprocessing and threading I am missing? Is this a bad idea? Should I stick with keeping the listener code in a separate script?
Thanks in advance!
The __init__
method for pyaudio.open()
is:
__init__(self, PA_manager, rate, channels, format, input=False, output=False, input_device_index=None, output_device_index=None, frames_per_buffer=1024, start=True, input_host_api_specific_stream_info=None, output_host_api_specific_stream_info=None)
According to the Docs on their website. You don't seem to be setting a PA_manager which looks like a required parameter.
精彩评论