【问题标题】:Tkinter and multiprocessing - cannot pickle '_tkinter.tkapp' objectTkinter 和多处理 - 不能腌制“_tkinter.tkapp”对象
【发布时间】:2021-12-11 20:28:04
【问题描述】:

当按下按钮时,我尝试从 tkinter UI 进行多处理,但出现错误:TypeError: cannot pickle '_tkinter.tkapp' object。

以下代码在我将 tkinter 对象更改为本地时有效(通过删除“self.”部分)。为什么会这样?

我发现了类似的帖子(下面的链接),据我了解,问题是不能腌制 tkinter 对象以在进程之间共享信息。我不明白为什么下面的代码会产生问题,因为我没有尝试从进程中更新 UI。

我想要做的事情可以实现吗?最好的做法是什么?

Python multiprocessing - TypeError: cannot pickle '_tkinter.tkapp' object

Cannot 'pickle' a Tkinter object in a multiprocessing environment in Windows

def main():
    main = MainClass()

class MainClass:

    def __init__(self):
        self.create_gui()

    def create_gui(self):
        self.ui_root = Tk()
        btn = Button(self.ui_root, text="Button", command=self.start_processsing_thread)
        btn.pack()
        self.ui_root.mainloop()

    def start_processsing_thread(self):
        process_thread = threading.Thread(target=self.dummy_processing)
        process_thread.start()

    def dummy_processing(self):
        with concurrent.futures.ProcessPoolExecutor() as executor:
            list = [1, 2, 3]
            results = executor.map(self.process, list)
            for number in results:
                print(number)
                end = time.perf_counter()
        print("Finished all")

    def process(self, secs):
        time.sleep(secs)
        return secs

if __name__ == '__main__':
    main()

编辑:

在搜索了如何使 GUI 在单个进程上运行之后(感谢 @Bryan Oakley 和 @Reblochon Masque 的回答),我想出了以下代码。我是 Python 的初学者,如果有人能评论这是一种很好的方法,我将不胜感激。

在用户按下按钮后,在我的实际应用程序中完成了一些图像处理(创建了文本)。处理完图像后,我需要将一些对象返回给 UI 进程。我发现这样做的方法是使用队列。但是如果使用 Queue 我不能像以前那样使用 concurrent.futures.ProcessPoolExecutor() 。据我了解,这有点不受欢迎,因为 ProcessPoolExecutor 知道如何将进程分配给处理器的核心,但是按照我现在的方式,一切都在单个核心上完成。我写的内容正确吗?有没有办法使用 ProcessPoolExecutor?

def main():
    main = UIClass()


class UIClass:
    def __init__(self):
        self.queue = Queue()
        self.create_gui()

    def create_gui(self):
        self.ui_root = Tk()
        btn = Button(self.ui_root, text="Button", command=self.button_pressed)
        btn.pack()
        self.ui_root.mainloop()

    def button_pressed(self):
        print("Button pressed")
        self.ui_root.after(1000, self.check_results)
        self.dummy_processing()

    def dummy_processing(self):
        nr_list = [1, 2, 3, 4, 5]
        for secs in nr_list:
            process = TestProcess(self.queue, secs)
            process.start()

    def check_results(self):
        print("Checking results")
        try:
            result = self.queue.get(timeout=0.1)
            print("Result is " + str(result))
        except Exception as e:
            print("No results yet, waiting again")
        self.ui_root.after(1000, self.check_results)


class TestProcess(Process):
    def __init__(self, queue, secs):
        Process.__init__(self)
        self.queue = queue
        self.secs = secs

    def run(self):
        print("Run in process")
        result = self.process(self.secs)
        self.queue.put(result)

    def process(self, secs):
        time.sleep(secs)
        print("Done sleeping")
        return secs


if __name__ == '__main__':
    main()

【问题讨论】:

  • 您能否将您的 GUI 与您的流程分开?您可能不需要多个 GUI 实例。

标签: python tkinter pickle


【解决方案1】:

Tkinter 对象不能跨越进程边界,这实际上是这个错误告诉你的。 Python使用pickle在进程之间传输对象,tkinter对象不能被pickle。

您需要对代码进行结构化,以便 100% 的 GUI 在单个进程中。

【讨论】:

    猜你喜欢
    • 2021-06-17
    • 2015-11-07
    • 2016-10-22
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    相关资源
    最近更新 更多