push.closeAll() for connection in connections: connection.close() def main(): try: init_thread() except Exception, e: closeAll() print "failed to initiate threads" print e sys.exit(0) if __name__ == "__main__": push = GCMLogic() THREADS = 5 connections = [] for thread in range(THREADS): try: connections.append(push.getConnection()) except MySQLdb.Error, e: print "Error %d: %s" % (e.args[0], e.args[1]) closeAll() sys.exit(1) main()