简体   繁体   English

在 Python 中使用 PyAudio 和 Tkinter 进行多处理不适用于 Mac OS X

[英]Multiprocessing with PyAudio and Tkinter in Python not working on Mac OS X

I'm trying to make a GUI application that incorporates both image and audio processing simultaneously using the multiprocessing library.我正在尝试使用多处理库制作一个同时包含图像和音频处理的 GUI 应用程序。 However the same code when tested on a Mac OS X does not seem to work, unless I remove the import tkinter statement但是,在 Mac OS X 上测试时相同的代码似乎不起作用,除非我删除import tkinter语句

import pyaudio
# The import statement below is causing the problem
import tkinter as tki
import multiprocessing
import threading

def do_task():
    p = pyaudio.PyAudio()
    print("Task complete")

if __name__ == '__main__':
    p = multiprocessing.Process(target=do_task, args=())
    p.start()
    # Code works when import statement is placed here
    # import tkinter as tki
    p.join()

The above code prints Task complete on Windows and also prints Task complete when the import statement is shifted, which is not a good solution as I'll need to start the multiprocessing after I initialize the UI.上面的代码打印任务在Windows上完成,还打印任务完成后,import语句被移动,这是不是因为我需要在我初始化UI开始多了良好的解决方案。

Is there any workaround to this problem?这个问题有什么解决方法吗?

It would be nice if we knew what exactly the Error is, so if you could copy it in it would be nice.如果我们知道错误到底是什么,那就太好了,所以如果你能把它复制进去就好了。 ^^ Anyways I don't have good experiences with multiprocessing.Process either so I use threading.Thread instead. ^^ 无论如何,我对multiprocessing.Process也没有很好的经验,所以我使用threading.Thread代替。 Therefor I define a new class with Thread as metaclass and define whatever the running function should be as run .因此,我用Thread作为元类定义了一个新类,并将运行的函数定义为run eg:例如:

from threading import Thread

class Audio(Thread):
    def __init__(self,...):
        Thread.__init__(self)
        #...
    def run(self):
        #audio stuff

class GUI(Thread):
    def __init__(self,...):
        Thread.__init__(self)
        #...
    def run(self):
        #GUI stuff
a,g=Audio(),GUI()
a.start(),g.start()

I don't know if that really helps but I hope so.我不知道这是否真的有帮助,但我希望如此。 :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM