title: Python multiprocessing - Is there a way to make sure python process stops after execution of the program
body: <p>Using pool with multi processing for hitting an api and storing the response into mysql for around 5k queries. The program never exits even after complete execution.Found that this happens as the processes are still open after execution. </p>
  
  <p>Tried using join(), terminate(). Nothing works.</p>
  
  <pre><code>    def fetchAndStore(query):
          conn = pymysql.connect(host=host,
                                   user=user,
                                   passwd=passwd,
                                   db=db)
  
          x = conn.cursor()
          full_url = getFullUrl(redirection_service, query)
          response = urllib2.urlopen(full_url)
          html = response.read()
          data = json.loads(html)
          if data is not None:
               store =  json.dumps(data["RESPONSE"]["redirectionStore"])
          else:
               store = 'search.flipkart.com'
          try:
              stmt = "INSERT INTO golden_response(`store`) VALUES ('%s')" %( store)
              x.execute(stmt)
              conn.commit()
              conn.close()
          except Exception, e:
              global failure
              failure += 1
              print e
            return "Done"
     #main
     queries = db_connection()
     pool = Pool(processes=20, maxtasksperchild=5)
     results = []
     for query in queries:
         results.append(pool.apply_async(fetchAndStore, (query,)))
  
     pool.close()
     pool.join()
     print "completed"
  </code></pre>
  
  <p>The process should exit and print completed, in the ideal situation.</p>