multithreading - Python - Running multiple mysql inserts without waiting for completion using a queue -


problem in basic form. have multidimensional list generating csv. have operates so:

data = [['hello', 'world'],['hello','universe']] in data:     try:         cursor.execute("""insert mytable (word1,word2) values ('%s','%s')""" % (i[0],i[1]))         cursor.execute("""insert random command here""" % ())         conn.commit()     except exception:         conn.rollback() 

this works. however, has wait response before attempts commit next one. takes fair amount of time complete going 1 @ time, hoping use queue/threading in order send multiple of these queries (10 or so) @ time while goes through list of several hundred of these.

i have read several tutorials on queues , multithreading, can't wrap head around how address specific items out of list (or how queuing , multithreading work). tried below (and couple of other variations) unable comprehend how work values being called way:

def stuff(q):     while true:         try:             cursor.execute("""insert mytable (word1,word2) values ('%s','%s')""" % (q.get(x[0]),q.get(x[1])))             cursor.execute("""random command here""" % ())             conn.commit()         exception exception:             conn.rollback()             cursor.execute(""""insert statement here""" % ())                     q.task_done()  q = queue(maxsize=0) num_threads = 2 array = [['hello','world'],['this','is']]  in range(num_threads):     worker = thread(target=stuff, args=(q,))     worker.setdaemon(true)     worker.start()  x in array:     q.put(x) 

i unsure of how work other items in array inside of "stuff" function, , result, have been guessing. have mediocre grasp of queue/threading, of tutorials have found googling single dimension arrays. insight appreciated, have been having hard time trying head around this. thanks.

edit: updated example more specific. have list of unique ticket numbers, first queries ones aren't in database. ones aren't in, puts mulidimensional list. try make insert live table, if 1 of key identifiers don't match, throws exception, , want them roll , insert different table

i ended packing variables q.put(), , splitting them inside function. guessing there better ways go, best figure out. rolled in blender's suggestion so. else:

def stuff(q):     while true:         try:             one, 2 = q.get().split(',')             cursor.execute("""insert mytable (word1,word2) values (%s,%s)""", (one,two)))             cursor.execute("""random command here""", ())             conn.commit()         exception exception:             conn.rollback()             cursor.execute(""""insert statement here""", ())                     q.task_done()  q = queue(maxsize=0) num_threads = 2 array = [['hello','world'],['this','is']]  in range(num_threads):     worker = thread(target=stuff, args=(q,))     worker.setdaemon(true)     worker.start()  x in array:     q.put(x[0]+','+x[1]) 

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 -