title: Simple Multithreading in Python
body: <p>I am new to python and try to execute two tasks simultanousely. These tasks are just fetching pages on a web server and one can terminate before the other. I want to display the result only when all requests are served. Easy in linux shell but I get nowhere with python and all the howto's I read look like black magic to a beginner like me. They all look over complicated to me compared with the simplicity of the bash script below.</p>
  
  <p>Here is the bash script I would like to emulate in python:</p>
  
  <pre><code># First request (in background). Result stored in file /tmp/p1
  wget -q -O /tmp/p1 "http://ursule/test/test.php?p=1&amp;w=5" &amp;
  PID_1=$!
  
  # Second request. Result stored in file /tmp/p2
  wget -q -O /tmp/p2 "http://ursule/test/test.php?p=2&amp;w=2"
  PID_2=$!
  
  # Wait for the two processes to terminate before displaying the result
  wait $PID_1 &amp;&amp; wait $PID_2 &amp;&amp; cat /tmp/p1 /tmp/p2
  </code></pre>
  
  <p>The <strong>test.php</strong> script is a simple:</p>
  
  <pre><code>&lt;?php
  printf('Process %s (sleep %s) started at %s ', $_GET['p'], $_GET['w'], date("H:i:s"));
  sleep($_GET['w']);
  printf('finished at %s', date("H:i:s"));
  ?&gt;
  </code></pre>
  
  <p>The bash script returns the following:</p>
  
  <pre><code>$ ./multiThread.sh
  Process 1 (sleep 5) started at 15:12:59 finished at 15:12:04
  Process 2 (sleep 2) started at 15:12:59 finished at 15:12:01
  </code></pre>
  
  <p>What I have tried so far in python 3:</p>
  
  <pre><code>#!/usr/bin/python3.2
  
  import urllib.request, threading
  
  def wget (address):
      url = urllib.request.urlopen(address)
      mybytes = url.read()
      mystr = mybytes.decode("latin_1")
      print(mystr)
      url.close()
  
  thread1 = threading.Thread(None, wget, None, ("http://ursule/test/test.php?p=1&amp;w=5",))
  thread2 = threading.Thread(None, wget, None, ("http://ursule/test/test.php?p=1&amp;w=2",))
  
  thread1.run()
  thread2.run()
  </code></pre>
  
  <p>This doesn't work as expected as it returns:</p>
  
  <pre><code>$ ./c.py 
  Process 1 (sleep 5) started at 15:12:58 finished at 15:13:03
  Process 1 (sleep 2) started at 15:13:03 finished at 15:13:05
  </code></pre>