title: Explanation of need for Multi Threading GUI programming
body: <p>I'm looking for a good explanation of the need to use multi-threading in graphical applications. In the examples below Python is used but the question is not python specific it applies to maybe the general design of graphical programming in any language.</p>
  
  <p>Lets take a simple example. Let's assume there is an application that does some sort of time consuming operation on a collection of files, and that it outputs it's progress to the console. Lets assume that this operation takes 2 seconds per file and that there are 10 files to process called 1.txt, 2.txt,3.txt, ... 10.txt. Then an example implementation could look like the following:</p>
  
  <p><strong>console</strong></p>
  
  <pre><code>import time
  def process(file):
      print 'processing {0}...'.format(file)
      time.sleep(2.0) #simulate slow operation
  
  files = ['{0}.txt'.format(i) for i in range(1, 11)]
  map(process, files)
  </code></pre>
  
  <p>The console example is of course single threaded and does the job just fine. Now if we wanted to add a graphical progress bar a single threaded implementation might look like the following:</p>
  
  <p><strong>single-threaded gui</strong></p>
  
  <pre><code>import time, gtk, gobject
  
  def process(file):
      print 'processing {0}...'.format(file)
      time.sleep(2.0)
  
  class MainWindow(gtk.Window):
      def __init__(self):
          super(MainWindow, self).__init__()
          self.progress = gtk.ProgressBar()
          self.progress.set_fraction(0)
          self.add(self.progress)
          self.connect("destroy", gtk.main_quit)
          self.show_all()
          files = ['{0}.txt'.format(i) for i in range(1, 11)]
          gobject.timeout_add(100, self.submit, files, 0)
  
      def submit(self, files, i):
          process(files[i])
          self.progress.set_fraction((i + 1.0)/len(files))
          if i + 1 &lt; len(files):
              gobject.idle_add(self.submit, files, i + 1)
  
  win = MainWindow()
  gtk.main()
  </code></pre>
  
  <p>This seems to work fine, but as you run the application if you try and interact with the application like try and resize the window for example it will get stuck and only respond every two seconds when it gets freed up to process pending gui events. The final example is a multi-threaded implementation and stays responsive throughout execution.</p>
  
  <p><strong>multi-threaded gui</strong></p>
  
  <pre><code>import time, gtk, gobject, threading
  
  def process(file):
      print 'processing {0}...'.format(file)
      time.sleep(2.0)
  
  class MainWindow(gtk.Window):
      def __init__(self):
          super(MainWindow, self).__init__()
          self.progress = gtk.ProgressBar()
          self.progress.set_fraction(0)
          self.add(self.progress)
          self.connect("destroy", gtk.main_quit)
          self.show_all()
          files = ['{0}.txt'.format(i) for i in range(1, 11)]
          threading.Thread(target=self.submit, args=(files,)).start()
  
      def submit(self, files):
          for i, file in enumerate(files):
              process(file)
              gobject.idle_add(self.progress.set_fraction, (i + 1.0)/len(files))
              if not self.get_visible():
                  return
  
  gtk.gdk.threads_init()
  win = MainWindow()
  gtk.main()
  </code></pre>
  
  <p>It seems perfectly clear and logical to me that if you have a long-running blocking operation in your code and you want a responsive gui that you have to use a multi-threaded solution. There is no other way around it. Is this the case? I have tried to explain this many times to fellow developers but many don't understand or don't agree. Can someone provide an explanation of this concept, a link to an article on it, or correct me if my understanding is incorrect. </p>