问题描述
我有一个wxPython GUI,并且想使用多重处理来创建一个单独的使用PyAudio的进程.也就是说,我想使用PyAudio,wxPython和多处理模块,但是尽管我可以使用其中任何两个,但不能同时使用这三个.具体来说,如果我从一个文件导入wx,并创建一个打开PyAudio的multiprocessing.Process
,则PyAudio将不会打开.这是一个示例:
文件:A.py
import wx
import time
use_multiprocessing = True
if use_multiprocessing:
from multiprocessing import Process as X
else:
from threading import Thread as X
import B
if __name__=="__main__":
p = X(target=B.worker)
p.start()
time.sleep(5.)
p.join()
文件:B.py
import pyaudio
def worker():
print "11"
feed = pyaudio.PyAudio()
print "22"
feed.terminate()
在所有测试中,我都看到11
打印,但是问题是我没有看到所示程序的22
.
- 如果我只注释掉
import wx
,我会看到22
和pyaudio加载 - 如果我仅设置
use_multiprocessing=False
,所以我改用线程,则会看到22
和pyaudio负载. - 如果我在
worker
中执行其他操作,它将运行(仅pyaudio无法运行)
我已经在Python 2.6和2.7中进行了尝试; PyAudio 0.2.4、0.2.7和0.2.8;和wx 3.0.0.0和2.8.12.1;我正在使用OSX 10.9.4
发生这种情况有两个原因,但它们看起来几乎相同.
无论哪种方式,根本问题是multiprocessing
只是 fork
来生一个孩子.
wx
内部的一些内部对象对其线程感到困惑.** 但是您不在乎为什么您的子进程陷入僵局.您想知道如何解决它.
一个简单的解决方案是,而不是尝试fork
然后清除所有不应该复制的内容,而是spawn
一个全新的Python进程,然后复制所有应该复制的内容. /p>
从Python 3.4开始,实际上有两个变体.有关详细信息,请参见上下文和启动方法,以及问题#8713 作为背景.
但是您使用的是2.6,所以这对您没有帮助.那你该怎么办?
最简单的答案是从multiprocessing
切换到第三方库 billiard
. billiard
是Python 2.7的multiprocessing
的分支,它添加了Python 3.x和Celery的许多功能和错误修复.
我相信新版本具有与Python 3.4完全相同的修复程序,但我并不乐观(对不起,我没有安装它,并且无法在线找到文档……).
import wx
import time
use_multiprocessing = True
if use_multiprocessing:
from multiprocessing import Process as X
else:
from threading import Thread as X
import B
if __name__=="__main__":
p = X(target=B.worker)
p.start()
time.sleep(5.)
p.join()
import pyaudio
def worker():
print "11"
feed = pyaudio.PyAudio()
print "22"
feed.terminate()
In all my tests I see 11
print, but the problem is that I don't see 22
for the program as shown.
- If I only comment out
import wx
I see22
and pyaudio loads - If I only set
use_multiprocessing=False
so I use threading instead, I see22
and pyaudio loads. - If I do something else in
worker
, it will run (only pyaudio doesn't run)
There are two reasons this can happen, but they look pretty much the same.
Either way, the root problem is that multiprocessing
is just fork
ing a child. This could be either causing CoreFoundation to get confused about its runloop*, or causing some internal objects inside wx
to get confused about its threads.**
But you don't care why your child process is deadlocking; you want to know how to fix it.
The simple solution is to, instead of trying to fork
and then clean up all the stuff that shouldn't be copied, spawn
a brand-new Python process and then copy over all the stuff that should.
As of Python 3.4, there are actually two variations on this. See Contexts and start methods for details, and issue #8713 for the background.
But you're on 2.6, so that doesn't help you. So, what can you do?
The easiest answer is to switch from multiprocessing
to the third-party library billiard
. billiard
is a fork of Python 2.7's multiprocessing
, which adds many of the features and bug fixes from both Python 3.x and Celery.
I believe new versions have the exact same fix as Python 3.4, but I'm not positive (sorry, I don't have it installed, and can't find the docs online…).
But I'm sure that it has a similar but different solution, inherited from Celery: call billiards.forking_enable(False)
before calling anything else on the library. (Or, from outside the program, set the environment variable MULTIPROCESSING_FORKING_DISABLE=1
.)
这篇关于Python多处理,PyAudio和wxPython的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!