Skip to content Skip to sidebar Skip to footer

Tkinter Application 'freezes' While Continually Polling Pipe For Contents (multiprocessing)

I have two scripts: Processor_child.py: Its purpose is to perform a number of data analysis and cleaning operations. This must perform the same operations when run alone (without T

Solution 1:

Your Connection.poll() call is busy-waiting and chewing through the CPU. But note that Connection objects have a fileno() method; this means you can use select/poll calls to put your process to sleep while waiting for them to become ready for I/O. Note that the tkinter event loop supports file handlers to allow you to do this without blocking the UI.

Solution 2:

Consider writing your app in a client-server fashion.

The client, is the Tk app, which can connect to the server. the server, simply executes whatever the client requires. this way, you can detach the processing. there are several ways you can do this, like cherrypy, rabbitmq and similar.

Recently, in desktops apps, I've used Electron, to connect to a cherrypy server, and AJAX requests from Electron using Javascript. the final icon simply starts both, the server and the client. this allows me to have a richer widget set, since the web is more powerful than Tk.

That will allow you in a possible future to have a webapp.

HTH

Solution 3:

It appears that the behaviour you are trying to achieve is to communicate with a function whilst it's running. I think that your problems could be solved by using generators. A generator lets you yield multiple values from a function, and send values to that function.

Here is some more information on generators if you want to know how they work.

I'm not entirely sure if this is exactly the behaviour you want from your program, but I have modified your code to use generators rather than multiprocessing, and it no longer freezes:

Processor_child.py:

### processor_child.py ###import pandas as pd
import time


defreview_with_user(var_names, dataset):
    affirmed = []
    review_message = 'Yes or no?'

    review_response = yield review_message

    if review_response in ['Yes', 'yes']:
        for v in dataset.columns:
            local_response = yieldstr(dataset[v].dropna())+"\n"+review_message

        yield affirmed

if __name__ == "__main__":
    var_names = ['var1', 'var2']
    df = pd.read_csv('dummy.csv')
    gen = review_with_user(var_names, df)
    # since it is now a generator, you need yo write some code to communicate with it via the console# it doesn't print to the console or recieve input unless you do this manuallywhileTrue:
        try:
            print(next(gen))
        except StopIteration:
            breakprint(gen.send(input()))

Tkinter_parent.py:

### Tkinter_parent.py ###
from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter import ttk
import pandas as pd
import Processor_child
import time

class GUI:
    def __init__(self, master):
        self.master = master

def gui_input(message, p_review):
    def input_done(event=None):
        entry.pack_forget()
        input_label.pack_forget()
        submit_button.pack_forget()
        try:
            p_review.send(entry.get())
            next_one(p_review)
        except StopIteration:
            # this code is executed when there is no more output from Processor_child.review_with_user
            return

    entry = Entry(frame)
    input_label = ttk.Label(frame, text=message)
    entry.bind("<Return>", input_done)
    submit_button = ttk.Button(frame, text="Submit", command=input_done)
    input_label.pack()
    entry.pack()
    submit_button.pack()

def file_select():
    dataset_path = askopenfilename()

    if __name__ == '__main__':
        some_vars = ['a var', 'another var']
        a_df = pd.read_csv(dataset_path)

        p_review = Processor_child.review_with_user(some_vars, a_df)

        gui_input(next(p_review), p_review)

def next_one(p_review):
    try:
        gui_input(next(p_review), p_review)
    except StopIteration:
        # this code is executed when there is no more output from Processor_child.review_with_user
        return

if __name__ == '__main__':
    root = Tk()
    my_gui = GUI(root)
    root.style = ttk.Style()
    root.style.configure('my.TButton')
    root.style.configure('my.TLabel')

    canvas = Canvas(root)
    frame = Frame(canvas)
    frame.place()
    canvas.pack(side="left", fill="both", expand=True)
    canvas.create_window((45,50), window=frame, anchor="nw")

    ttk.Button(frame, text="Select", command=file_select).pack()

    root.mainloop()

Generators will throw a StopIteration exception when you call next() on them and they have finished, so be sure to put next(p_review) and and p_review.send(...) calls inside try blocks where appropriate.

Post a Comment for "Tkinter Application 'freezes' While Continually Polling Pipe For Contents (multiprocessing)"