PrivateUser
PrivateUser

Reputation: 4524

Python mysqldb db.commit() not working

I have some code like this

db = MySQLdb.connect(host="localhost", user="root", passwd="root", db="domains")
cursor = db.cursor()

def check_urls(res):
    pool = Pool(25)
    for row in res:
        pool.spawn(fetch, row[0], row[1])
    pool.join()

def fetch(*args):
    """code trimmed for brevity"""
    cursor.execute("""UPDATE com SET http_status=%s, is_checked=1 WHERE id=%s""",
                  (output.get('http_status', ""), id))


for _ in xrange(10000):
    cursor.execute("SELECT domain, id FROM com WHERE is_checked IS NULL LIMIT 100")
    result = cursor.fetchall()
    check_urls(result)
    db.commit()


cursor.close()
db.close()

My program get stuck at db.commit(). No values updated in the database. Can someone tell me whats wrong?.

Please note: My check_urls function has a for loop and each loop has one mysql update query.

Upvotes: 3

Views: 2420

Answers (1)

valignatev
valignatev

Reputation: 6316

It seems like you should move your db.commit() into fetch function after cursor.execute. Another way you may use "global" keyword with cursor. Or, third way, just make cursor as parameter of functions.

Upvotes: 1

Related Questions