Home > Software engineering >  Python Multiprocessing with tkinter on Windows
Python Multiprocessing with tkinter on Windows

Time:08-04

I'm using the multiprocessing library from Python inside a Tkinter GUI to run real-time calculations independent from my GUI. I spawn the process in the following way

pti_process = multiprocessing.Process(target=self.pti.run_live, daemon=True,
                                      args=(self.live_path, self.settings.data))
pti_process.start()

This works fine on Linux. However, on Windows I got directly after spawning the process the following two exceptions:

TypeError: cannot pickle '_thread.lock' object
EOFError: Ran out of input

The code shown above is literally the only thing that I'm doing regarding multiprocessing. I do not use any lock. With threads works everything fine.

CodePudding user response:

Check this post out on the differences between multiprocessing on Linux and Windows.

On Windows, multiprocessing.Process does not support bound or unbound methods as targets, so you probably need to redefine self.pti.run_live as a function -- I believe this (the fact that self.pti.run_live is a method and thus is not picklable) is the source of the error, but it's a bit hard to tell without seeing more of your code.

Threads do not require picklable targets, so it makes sense that this works fine with threads instead of processes.

It's also worth noting that on Windows (as opposed to POSIX systems) processes are started with spawn rather than fork and so global state is not maintained in new processes.

I hope this helps.

  • Related