通过 python 多处理启动子进程挂起

发布于 2024-11-11 16:06:05 字数 1325 浏览 6 评论 0原文

我正在使用 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)

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

提前致谢!

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,
                        channels = 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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光清浅 2024-11-18 16:06:05

pyaudio.open()__init__ 方法是:

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文