Implementing threading in a Python GTK application (PyGObject) to prevent UI freezing

Question:

Simply put, I want to properly implement threading in a Python GTK application. This is in order to prevent UI freezing due to functions/code taking a long time to finish running. Hence, my approach was to move all code which took a long time to run into separate functions, and run them in their separate threads as needed. This however posed a problem when trying to run the functions in sequence.
For example, take a look at the following code:

class Main(Gtk.Window):
    def __init__(self):
        super().__init__()

        self.button = Gtk.Button(label='button')
        self.add(self.button)
        self.button.connect('clicked', self.main_function)


    def threaded_function(self):
        time.sleep(20)
        print('this is a threaded function')


    def first_normal_function(self):
        print('this is a normal function')


    def second_normal_function(self):
        print('this is a normal function')


    def main_function(self, widget):
        self.first_normal_function()
        self.threaded_function()
        self.second_normal_function()

Pressing the button starts main_function which then starts 3 functions in sequence. threaded_function represents a function which would take a long time to complete. Running this as is will freeze the UI. Hence it should be threaded as such:

...
...

def main_function(self, widget):
    self.first_normal_function()
    
    thread = threading.Thread(target=self.threaded_function)
    thread.daemon = True
    thread.start()
    
    self.second_normal_function()

What should happen is that the following first_normal_function should run, then threaded_function in a background thread – the UI should remain responsive as the background thread is working. Finally, second_normal_function should run, but only when threaded_function is finished.

The issue with this is that the functions will not run in sequence. The behaviour I am looking for could be achieved by using thread.join() however this freezes the UI.

So I ask, what’s the proper way of doing this? This is a general case, however it concerns the general issue of having code which takes a long time to complete in a graphical application, while needing code to run sequentially. Qt deals with this by using signals, and having a QThread emit a finished signal. Does GTK have an equivalent?

I’m aware that this could be partially solved using Queue , with a put() and get() in relevant functions, however I don’t understand how to get this to work if the main thread is calling anything other than functions.

EDIT: Given that it’s possible to have threaded_function call second_normal_function using GLib.idle_add, let’s take an example where in main_function, the second_normal_function call is replaced with a print statement, such that:

def main_function(self, widget):
    self.first_normal_function()
    
    thread = threading.Thread(target=self.threaded_function)
    thread.daemon = True
    thread.start()
    
    print('this comes after the thread is finished')
    ...
    ...
    ...
    #some more code here

With GLib.idle_add, the print statement and all the code afterwards would need to be moved into a separate function. Is it possible to avoid moving the print statement into its own function while maintaining sequentiality, such that the print statement remains where it is and still gets called after threaded_function is finished?

Asked By: user_968563

||

Answers:

Your suggestion on how to do this was very close to the actual solution, but it’s indeed not going to work.

In essence, what you’ll indeed want to do, is to run the long-running function in a different thread. That’ll mean you get 2 threads: one which is running the main event loop that (amongs other things) updates your UI, and another thread which does the long-running logic.

Of course, that bears the question: how do I notify the main thread that some work is done and I want it to react to that? For example, you might want to update the UI while (or after) some complex calculation is going on. For this, you can use GLib.idle_add() from within the other thread. That function takes a single callback as an argument, which it will run as soon as it can ("on idle").

So a possibility to use here, would be something like this:

class Main(Gtk.Window):

    def __init__(self):
        super().__init__()

        self.button = Gtk.Button(label='button')
        self.add(self.button)
        self.button.connect('clicked', self.main_function)

        thread = threading.Thread(target=self.threaded_function)
        thread.daemon = True
        thread.start()

    def threaded_function(self):
        # Really intensive stuff going on here
        sleep(20)

        # We're done, schedule "on_idle" to be called in the main thread
        GLib.idle_add(self.on_idle)

    # Note, this function will be run in the main loop thread, *not* in this one
    def on_idle(self):
        second_normal_function()
        return GLib.SOURCE_REMOVE # we only want to run once

    # ...

For more context, you might want to read the pygobject documentation on threading and concurrency

Answered By: nielsdg