3

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.

4
  • Are you getting any errors? What does "stuck at db.commit()" mean? Is the program stuck or do you mean that part doesn't happen? Commented May 17, 2014 at 20:36
  • 2
    It appears as though you might be spawning 25,000 threads and joining the results back together just before the commit. Are you sure the commit is the problem? It's not that likely... Commented May 17, 2014 at 20:36
  • @yuvi No i'm not getting any errors. I checked my MySQL error logs. No errors. I mean its running only the first loop. Even that first loop no values get updated in the database Commented May 17, 2014 at 20:48
  • @Ben 25000 threads? I'm selecting only 100 domains and passing it into the pool. Commented May 17, 2014 at 20:51

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.