I am trying to create an application where a Tkinter GUI is updated by other objects that are continuously taking data. I was having issues using multithreading so I decided to try and use the multiprocessing module.
I've found that you cannot run a Tkinter window inside of a multiprocessing.Process
, here is the minimum example:
import Tkinter as tk
import multiprocessing
class Subprocess(multiprocessing.Process):
def __init__(self):
multiprocessing.Process.__init__(self)
self.root = tk.Tk()
#
def run(self):
self.root.mainloop()
#
def stop(self):
self.root.destroy()
self.terminate()
if __name__ == '__main__':
process = Subprocess()
process.start()
print "I got around the global interpreter lock"
raw_input()
print "exiting"
process.stop()
What I expect to happen is for a Tk window to pop up and "I got around the global interpreter lock" to show up in the terminal. I tested this out on ubuntu linux and it worked fine, but when I switched over to Windows 7 (where I am developing my application) it failed giving me the error:
Traceback (most recent call last):
File "C:\pathtoscript\multiprocessing_test.py", line 21, in <module>
process.start()
File "C:\Python27\lib\multiprocessing\process.py", line 130, in start
self._popen = Popen(self)
File "C:\Python27\lib\multiprocessing\forking.py", line 277, in __init__
dump(process_obj, to_child, HIGHEST_PROTOCOL)
File "C:\Python27\lib\multiprocessing\forking.py", line 199, in dump
ForkingPickler(file, protocol).dump(obj)
File "C:\Python27\lib\pickle.py", line 224, in dump
self.save(obj)
File "C:\Python27\lib\pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:\Python27\lib\pickle.py", line 419, in save_reduce
save(state)
File "C:\Python27\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python27\lib\pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "C:\Python27\lib\pickle.py", line 681, in _batch_setitems
save(v)
File "C:\Python27\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python27\lib\pickle.py", line 725, in save_inst
save(stuff)
File "C:\Python27\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python27\lib\pickle.py", line 649, in save_dict
self._batch_setitems(obj.iteritems())
File "C:\Python27\lib\pickle.py", line 681, in _batch_setitems
save(v)
File "C:\Python27\lib\pickle.py", line 313, in save
(t.__name__, obj))
PicklingError: Can't pickle 'tkapp' object: <tkapp object at 0x02BD3D08>
Does anyone know a workaround to this? It seems odd to me that this works on linux but not on Windows.