Python code not executing in order? MySQLdb UPDATE commits in unexpected order -


i've got python 2.7 script i'm working on retrieves rows mysql table, loops through data process it, supposed following things in order:

  1. update table rows got set locked value in each row true

  2. after update query executes , commits via mysqldb, threadpool of processes should run on data original loop.

what's happening update query seems committing somehow after threadpool done. tried refactoring try/finally statement make sure, either still afterwards, or doesn't commit update , runs threadpool anyways.

it's head scratcher, sure. assume i'm doing wrong , obvious not catching after looking @ long. input appreciated!

here's gist:

from multiprocessing.pool import threadpool, imapiterator  import mysqldb mdb import os, sys, time import re  boto.s3.connection import s3connection boto.s3.bucket import bucket   ...  con = mdb.connect('localhost', 'user', 'pass', 'db')   con:       cur = con.cursor()     cur.execute("select preview_queue.filename, preview_queue.product_id, preview_queue.track, products.name, preview_queue.id preview_queue join `catalog_module-products` products on products.id = preview_queue.product_id locked != 1")      rows = cur.fetchall()     mp3s_to_download = []     lock_ids = []     last_directory = ""      if len(rows) > 0:         row in rows:             base_dir = str(get_base_dir(row[1], row[3]))             mp3s_to_download.append([base_dir, str(row[0])])              if last_directory != "preview_temp/"+base_dir:                 if not os.path.exists("preview_temp/"+base_dir):                     try:                         os.makedirs("preview_temp/"+base_dir)                      except oserror, e:                         pass              last_directory = "preview_temp/"+base_dir                lock_ids.append(str(row[4]))          if len(lock_ids) > 0:             action_ids = ','.join(lock_ids)              try:                 cur.execute("update preview_queue set locked = 1 id in ({})".format(action_ids))                 con.commit()             finally:                 pool = threadpool(processes=20)                 pool.map(download_file, mp3s_to_download)                   cur.close() 

a finally clause guaranteed execute, if try clause raises exception. happening here exception being raised, preventing update being committed, threads triggered anyway.

this doesn't seen appropriate use of try/finally. rather, use normal try/except catch , log exceptions, , perhaps use else clause start threads if no exception raised.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -