Home > Articles > Open Source > Python

Writing Multithreaded GUI Applications with Python

  • Print
  • + Share This
Want to take advantage of multithreading and side-step its associated obstacles? Boudewijn Rempt uses the PyQt toolkit to help you work around the challenges of programming a multithreaded GUI in Python.
Like this article? We recommend

Like this article? We recommend

Multithreading is a programming paradigm that is useful in a number of situations. GUI applications often need to present the appearance of doing several things at once, such as accepting user input while printing, showing a progress bar while computing, and downloading stuff while rendering what has already arrived. Complex games need threads to keep track of both player characters and nonplayer characters. Networking applications need to be able to serve more than one request at a time. Database applications need to monitor the application state while storing and retrieving data.

Because multithreading is so important in so many areas, it's a pity that working with it is often difficult. Debugging a multithreaded application is what programmer's nightmares are made of. The number of subtle timing bugs that can occur in even a simple multithreaded application is hard to underestimate. Threads also are a limited commodity on any operating system: Running out of available threads seldom leads to a pretty result.

Of course, there are alternatives to using threads. In Python, you have the option to use the asyncore module if you are working on a networking application. If you potentially need thousands of threads, you can turn to Stackless Python (see my InformIT article on this topic). A similar package is available for Java in the form of the JRockit JVM from Bea. And for less complex purposes—say, a progress bar or a simple periodical check of some state in your application—using a timer probably is sufficient.

But for your daily bread-and-butter programming, you will probably need threading at some point. Since the early days of NextSTEP, multithreading has grown up and it is now a mature technology.

In this article, I briefly outline the choices you have when you want to work with multithreading in Python while working on a GUI application. Then, using the PyQt toolkit, I will show you how to work around the various obstacles you can encounter when programming a multithreaded GUI.

  • + Share This
  • 🔖 Save To Your Account