37,743
社区成员




import threading
import MySQLdb
from time import sleep,ctime
db_server_index={0:'192.168.16.53',1:'192.168.17.225',2:'192.168.17.226'}
def get_rows(i,index_id):
print 'start loop','at:',ctime()
db_ip=db_server_index[index_id]
print db_ip
mysql_conn=MySQLdb.connect(host=db_ip,user="root",passwd="123445",db="test",charset="utf8")
mysql_cursor = mysql_conn.cursor()
mysql_cursor.execute("select @@server_id;")
for row in mysql_cursor.fetchall():
print row
print 'loop','done at:',ctime()
def get_rows_1(db_ip):
print 'start loop','at:',ctime()
print db_ip
mysql_conn=MySQLdb.connect(host=db_ip,user="root",passwd="meizu2013WSX_XCD.com",db="test",charset="utf8")
mysql_cursor = mysql_conn.cursor()
mysql_cursor.execute("select @@server_id;")
for row in mysql_cursor.fetchall():
print row
print 'loop','done at:',ctime()
def main():
print 'starting at:',ctime()
threads=[]
nloops=db_server
print nloops
for i in nloops:
t=threading.Thread(target=get_rows_1,args=(i,))
threads.append(t)
for i in nloops:
threads[i].start()
for i in nloops:
threads[i].join()
print 'all done at:',ctime()
if __name__=='__main__':
main()
starting at: Thu Oct 31 13:33:55 2013
('192.168.16.53', '192.168.17.225', '192.168.17.226', '192.168.16.55', '192.168.16.56', '192.168.16.57', '192.168.16.222', '192.168.17.228', '192.168.16.127', '192.168.16.128', '192.168.16.129', '192.168.16.242', '192.168.17.232', '192.168.17.242', '192.168.16.212', '192.168.16.241')
Traceback (most recent call last):
File "thread.py", line 54, in <module>
main()
File "thread.py", line 42, in main
threads[i].start()
TypeError: list indices must be integers, not str