下面是我用来将数据上传到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字符串设置在顶部。我曾尝试在工作线程中使用cxn字符串,但有这么大的改进。在工作线程中,MySQL是单向的。表被截断,然后插入。通常每个工人头上只有一个表.它速度很快,而且系统是本地的。但我看不到多个连接,我希望看到。
队列= 30-400个项目。
发布于 2011-04-02 02:56:07
您的队列中有多少项?
所有的操作都在同一张表上吗?如果是这样的话,如果由于表上的锁而使用了select和insert/updates/delete,多线程可能没有帮助。
从你的例子中,我们看不到你在哪里创建你的连接。它是在每个线程中创建的,还是对所有线程使用相同的连接?
有了25个线程,您的线程也可能会争夺队列上的锁。
https://stackoverflow.com/questions/5517293
复制相似问题