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

Viewed 934

I am trying to do simple multiprocessing with python and Tkinter. But I am receiving error.

Exception in Tkinter callback
...
...
    ForkingPickler(file, protocol).dump(obj)
TypeError: cannot pickle '_tkinter.tkapp' object

Program is simple. After run it opens window (startpage) where i click on button which redirects me to experimentPage where i click on button and everything starts

class experimentPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        # start experiment on click
        self.button2 = tk.Button(self, text="Ready",
                            command=self.logic)
        self.button2.pack()

    def proc1(self):
        while True:
            print("LOL1")

    def proc2(self):
        while True:
            print("LOL2")

    def proc3(self):
        while True:
            print("LOL3")

    def logic(self):
        t1 = multiprocessing.Process(target=self.proc1)
        t2 = multiprocessing.Process(target=self.proc2)
        t3 = multiprocessing.Process(target=self.proc3)
        t1.start()
        t2.start()
        t3.start()
        t1.join()
        t2.join()
        t3.join()

And here is my main

class Main(tk.Tk):

    def __init__(self, *args, **kwargs):
        
        tk.Tk.__init__(self, *args, **kwargs)
        container = tk.Frame(self)
        self.attributes('-fullscreen', True) 
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (startPage.startPage, experimentPage.experimentPage):

            frame = F(container, self)

            self.frames[F] = frame

            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(startPage.startPage)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()

app = Main()
app.mainloop()

I just cant make it work. Thanks for any help.

1 Answers

Multiprocessing uses multiprocessing.Queue to exchange information between processes. This queue implementation uses pickle to serialize and deserialize information.

Some objects, including tkinter's app are not picklable.

See this SO question for reference.

Related