Search code examples
python-2.7tkinterbackground-processpython-multithreading

Threading halts execution of Tkinter program


Hey all I'm writing an interface that works with jenkins to trigger build jobs and deployments. One feature I have been stuck on is being able to get the status of a build once it has been completed.

As of now I have a GUI implemented with Tkinter and the app is fully functioning except for the missing information on the final build status.

I was trying to poll jenkins for the information but I need to give it time to finish the build before polling. I thought I could accomplish this through a simple thread and just have it run in the background, however, when the thread is run and it hits the time.sleep() function it halts the rest of the program as well.

Is this possible to do without halting the rest of the program i.e. the GUI, and if so, where am i going wrong?

Here's a snippit of the problem area:

def checkBuildStatus(self):
    monitor_thread = threading.Thread(target=self._pollBuild())
    monitor_thread.daemon = True
    monitor_thread.start()

def _pollBuild(self):
    # now sleep until the build is done
    time.sleep(15)

    # get the build info for the last job
    build_info = self.server.get_build_info(self.current_job, self.next_build_number)
    result = build_info['result']

Solution

  • When you create a thread, you need to pass the function itself. Make sure to not call the function.

    monitor_thread = threading.Thread(target=self._pollBuild())
    #                                                       ^^
    

    Should be:

    monitor_thread = threading.Thread(target=self._pollBuild)