I am trying to understand multithreading and trying to execute following code but getting the error. Please help resolve this.
from tkinter import *
from tkinter.ttk import *
import tkinter as tk
import datetime
import multiprocessing
process1 = None
class App:
def __init__(self):
self.root = Tk()
self.top_frame = tk.Frame(self.root, height=50, pady=3)
self.selectFile = tk.Button(self.top_frame, text="Start", activebackground="blue",
command=lambda: self.create_process()).pack()
self.progressbar_frame = tk.Frame(self.root)
self.pgbar = Progressbar(self.progressbar_frame, length=125, orient=HORIZONTAL, mode="indeterminate")
self.pgbar.pack()
self.top_frame.pack()
self.root.mainloop()
def calculate_data(self):
a = datetime.datetime.now()
i = 0
while i < 100000000:
i+=1
print(i)
b = datetime.datetime.now()
print(b - a)
def create_process(self):
#self.pgbar_start()
global process1
process1 = multiprocessing.Process(target=self.calculate_data, args=())
process2 = multiprocessing.Process(target=self.pgbar_start, args=())
process1.start()
process2.start()
self.periodic_call()
def pgbar_start(self):
self.progressbar_frame.pack()
self.pgbar.start(10)
def pgbar_stop(self):
self.pgbar.stop()
self.progressbar_frame.pack_forget()
def periodic_call(self):
if process1.is_alive():
self.pgbar.after(1000, self.periodic_call)
else:
self.pgbar_stop()
if __name__ == "__main__":
app = App()
Following error I am getting:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Python Automation/Practice/multi_processing.py", line 15, in <lambda>
command=lambda: self.create_process()).pack()
File "C:/Python Automation/Practice/multi_processing.py", line 37, in create_process
process1.start()
File "C:\Program Files\Python37\lib\multiprocessing\process.py", line 112, in start
self._popen = self._Popen(self)
File "C:\Program Files\Python37\lib\multiprocessing\context.py", line 223, in _Popen
return _default_context.get_context().Process._Popen(process_obj)
File "C:\Program Files\Python37\lib\multiprocessing\context.py", line 322, in _Popen
return Popen(process_obj)
File "C:\Program Files\Python37\lib\multiprocessing\popen_spawn_win32.py", line 89, in __init__
reduction.dump(process_obj, to_child)
File "C:\Program Files\Python37\lib\multiprocessing\reduction.py", line 60, in dump
ForkingPickler(file, protocol).dump(obj)
TypeError: can't pickle _tkinter.tkapp objects
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files\Python37\lib\multiprocessing\spawn.py", line 105, in spawn_main
exitcode = _main(fd)
File "C:\Program Files\Python37\lib\multiprocessing\spawn.py", line 115, in _main
self = reduction.pickle.load(from_parent)
EOFError: Ran out of input
Please help me to get the understanding what I am doing wrong. My aim is to run progress bar in the tkinter window with background process. Progress bar should be running smooth.
tkinterstuff from that process (same with threads) but there is at least one case where threading wouldn't work, at least kinda. Threads use the same resources as the whole process, so if you have in the main process tkinter and a thread or multiple that consume the same resources and do it a lot it may leave tkinter with less of these resources and it may become very laggy, so you can span this thing to multiple processes who have their own resources