title: Main thread in Python
body: <p>I have a long program with a lot of threads in it. I created a sample, short code where you can see exactly what the problem is.</p>
  
  <pre><code>from threading import Thread, active_count
  import time
  
  def func(x):
      time.sleep(x)
      print(active_count())
  
  print(active_count())
  t = Thread(target=func, name="thread", args=(5,))
  t.start()
  print("main thread finished")
  </code></pre>
  
  <p>Simply put - I do not understand why the main thread is still active even after the last print statement was executed.</p>
  
  <ol>
  <li>I defined a function func</li>
  <li>The first print statement prints number 1 - number of active threads. Since the thread t has not been activated yet, number 1 is correct (only main thread)</li>
  <li>I defined thread t and executed it.</li>
  <li>While the thread t sleeps, the main thread finished the last print statement and SHOULD FINISH 
  BUT</li>
  <li>the thread t wakes up and prints the number of currently active threads - and the number is 2 - both the main thread and thread t are (according to python) active even though the main thread should have already finished and therefore should not be active anymore because it already printed the last line (the last print statement).</li>
  </ol>
  
  <p>Can please someone explain to me why is that and what is the mechanism behind the main thread vs. other threads?  thank you</p>