下面是我用来上传数据到MySQL的多线程脚本。使用线程执行多个插入,听起来不错。在
但是性能没有提高。MySql被设置为接受多个连接,但是当我检查进程列表时,我没有看到我期望的5-10个连接。cxn字符串是
有办法解决这个问题吗?在import sys, threading, Queue pyodbc
class WorkerThread(threading.Thread):
def __init__(self, queue):
threading.Thread.__init__(self)
self.queue = queue
def run(self):
while 1:
try: # take a job from the queue
id, null, null2, null3 = self.queue.get_nowait()
except Queue.Empty:
raise SystemExit
In Here I have MySQl connecctions
*** cxn = pyodbc.connect('DSN=MySQL;PWD=MLML;Option=3')
csr = cxn.cursor()
typical insert , selects Updates
if __name__ == '__main__':
connections = 25
# build a queue with tuples
queue = Queue.Queue()
queue.put(row[:3])
# print queue
threads = []
for dummy in range(connections):
t = WorkerThread(queue)
t.start()
threads.append(t)
# wait for all threads to finish
for thread in threads:
thread.join()
Cxn字符串设置在顶部。我曾尝试在Worker线程中使用cxn字符串,但是有很多改进。在工作线程中,MySQL是单向的。表被截断然后插入。通常每个工人只有一张桌子。它的速度很快,而且系统是本地的。但我看不到很多联系,我希望看到。在
队列=30-400个项目。在