python 多线程的问题

珠海-天堂 2013-10-31 01:34:29

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

...全文
87 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧