所以我在使用Python启动多个进程时遇到了麻烦。我将使用一个进程来运行下载,另一个进程用于显示进度条。现在,我正在制作进度条流程,但我不断收到这个奇怪的错误。我认为这与我使用Mac OS X的事实有关。
这是我的代码:
import Tkinter as tk
from multiprocessing import Process
class Application(tk.Frame):
def __init__(self, master = None):
tk.Frame.__init__(self,master)
self.grid()
self.setup()
def setup(self):
self.label = tk.Label(self, text = "Progress Bar")
self.label.grid()
self.canvas = tk.Canvas(self, cursor = 'arrow', height = 400, width = 400)
self.canvas.grid()
self.rectangle = self.canvas.create_rectangle(0,0,399,20,fill = 'white')
self.canvas.grid()
def appStart():
app = Application()
app.master.title("Progress Bar")
app.mainloop()
p = Process(target = appStart)
p.start()
这是错误:
The process has forked and you cannot use this CoreFoundation functionality safely. You MUST exec().
Break on __THE_PROCESS_HAS_FORKED_AND_YOU_CANNOT_USE_THIS_COREFOUNDATION_FUNCTIONALITY___YOU_MUST_EXEC__() to debug.
所以是的,我该如何解决这个问题。我真的想使用多处理库,因为它非常简单易用。关于并发性,我也是一个初学者,所以低级API对我来说真的很复杂。我需要使用多个进程,因为Python无法同时运行多个线程。请帮忙。
答案 0 :(得分:1)
您无法将GUI分成两个这样的进程。您需要将所有GUI代码保留在主进程中,并让其他进程通过队列与之通信。
答案 1 :(得分:0)
在OS 10.8.5中,我的代码存在同样的问题。
试试这个:
from multiprocessing import Process
def appStart():
import Tkinter as tk
class Application(tk.Frame):
def __init__(self, master = None):
tk.Frame.__init__(self,master)
self.grid()
self.setup()
def setup(self):
self.label = tk.Label(self, text = "Progress Bar")
self.label.grid()
self.canvas = tk.Canvas(self, cursor = 'arrow', height = 400, width = 400)
self.canvas.grid()
self.rectangle = self.canvas.create_rectangle(0,0,399,20,fill = 'white')
self.canvas.grid()
app = Application()
app.master.title("Progress Bar")
app.mainloop()
if __name__ == '__main__':
p = Process(target = appStart)
p.start()
在Mac和Linux中,您似乎只能在开始(分叉)流程后导入Tkinter,在这里我已经使用了您的" appStart"用作创建过程的Tkinter包装器。
一些参考:
Python Tkinter application causes fork() / exec() error on Mac OS X
Why doesn't tkinter play nicely with multiprocessing?