Actually the two processes are using separate queue
objects because queue
is not initialised inside if __name__ == "__main__"
block.
Create queue
inside the if statement and pass it to the two processes using args
option of Process(...)
:
from tkinter import *
from multiprocessing import Process, Queue
import time
class GUI:
def __init__(self, master, queue):
self.master = master
self.frame = Frame(self.master)
self.frame.grid()
self.queue = queue
self.button = Button(self.master, text="Update", command=self.update, bg="red")
self.button.grid(row=0, column=0)
def update(self):
self.queue.put(100)
print("I've inserted 100 into the queue")
# print("I've read and deleted the queue value: " + str(queue.get()))
def start_ui(queue):
root = Tk()
root.title = "Test this bitch error"
GUI(root, queue)
root.mainloop()
def work(queue):
print("Loop is starting")
while True:
print("Here is the inserted value", queue.get())
time.sleep(1)
if __name__ == "__main__":
queue = Queue()
ui_process = Process(target=start_ui, args=[queue])
work_process = Process(target=work, args=[queue])
ui_process.start()
work_process.start()
You don't need to create another process to run start_ui()
, just run it in current process:
if __name__ == "__main__":
queue = Queue()
work_process = Process(target=work, args=[queue])
work_process.start()
start_ui(queue)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…