通过 python 多处理启动子进程挂起
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
pyaudio.open()
的__init__
方法是:根据其网站上的文档。您似乎没有设置一个看起来像必需参数的 PA_manager 。
The
__init__
method forpyaudio.open()
is:According to the Docs on their website. You don't seem to be setting a PA_manager which looks like a required parameter.