Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

python - How to share data in multiprocessing via queue with a tkinter and worker function

in my project I'm using multiprocessing and I need to send tkinter ui values to two working processes. If there is no value in queue the while True loop of the working functions should go on.

At the moment I can use queue.put() (and for testing in the tkinter queue.get() to check if the queue is filled) but I don't get any value in my working processes.

I've tried to build a short program with the same problem and a similar structure:

from tkinter import *
from multiprocessing import Process, Queue
import time

queue = Queue()


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():
    root = Tk()
    root.title = "Test this bitch error"
    GUI(root, queue)
    root.mainloop()


def work():
    while True:
        print("Loop is starting")
        while not queue.empty():
            print("Here is the inserted value" + queue.get())
        time.sleep(1)


if __name__ == "__main__":
    ui_process = Process(target=start_ui)
    work_process = Process(target=work)

    ui_process.start()
    work_process.start()

Maybe you can find the problem and tell me how to fix it.

Best regards and thanks advance

question from:https://stackoverflow.com/questions/65886825/how-to-share-data-in-multiprocessing-via-queue-with-a-tkinter-and-worker-functio

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...