title: when will main thread exit in python
body: <p>I am reading <code>The Python Standard Library by Example</code> and get confused when I arrived page 509. </p>
  
  <blockquote>
    <p>Up to this point, the example programs have implicitly waited to exit until all threads have completed their work. Programs sometimes spawn a thread as a daemon that runs without blocking the main program from exiting. </p>
  </blockquote>
  
  <p>but after I run some codes, I get result that is opposite. The code is like this:</p>
  
  <pre><code>    #!/usr/bin/env python
  # encoding: utf-8
  #
  # Copyright (c) 2008 Doug Hellmann All rights reserved.
  #
  """Creating and waiting for a thread.
  """
  #end_pymotw_header
  
  import threading
  import time
  
  def worker():
      """thread worker function"""
      print 'Worker'
      # time.sleep(10000)
      return
  
  threads = []
  for i in range(5):
      t = threading.Thread(target=worker)
      threads.append(t)
      t.start()
  
  print "main Exit"
  </code></pre>
  
  <p>and sometime the result is this:</p>
  
  <pre><code>Worker
  Worker
  WorkerWorker
  
  main Exit
  Worker
  </code></pre>
  
  <p>So I want to ask when will main thread exit in python after it starts several thread?</p>