4

我正在使用 pyAudio 收听音频设备,并在主程序继续运行时在后台做一些“事情”。

我开始使用第二个脚本,但为了可支持性,我想合并到一个脚本中。当我将函数移入并使用 Process 启动侦听器时,它只是挂起并且从不运行。

这是简化的代码片段:

        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()

现在,如果我删除以下流设置,那么我将按预期返回进程 ID:

           stream = p.open(format = FORMAT,
                        channels = CHANNELS,
                        rate = RATE,
                        input = True,
                        frames_per_buffer = INPUT_FRAMES_PER_BLOCK)

我缺少关于多处理和线程的东西吗?这是一个坏主意吗?我应该坚持将侦听器代码保留在单独的脚本中吗?

提前致谢!

4

1 回答 1

1

__init__方法pyaudio.open()是:

__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) 

根据他们网站上的文档。您似乎没有设置看起来像必需参数的 PA_manager 。

于 2011-08-24T17:58:43.203 回答