多线程导致mysql提交错误_多线程中mysql插入出错

在写爬虫中,我要把爬取到的数据存到数据库中.每一个页面里边有很多条目,比如一个人的访客可能有很多个,于是插入卸载循环中,

try:

sql_visitor='INSERT INTO visitor (ownername,owneruid,visitorname,visitoruid,visittime) VALUE ("%s",%d,"%s",%d,"%s")'%(ownername,owneruid,visitorname,visitoruid,visitortime)

print sql_visitor

self.cursor.execute(sql_visitor)

self.connect.commit()

except Exception as e:

print e

一个页面一个线程,嫌弃慢的我开了5个

max_threads=5

while uid < 8000000 or threadlist:

for thread1 in threadlist:

if not thread1.is_alive():

threadlist.remove(thread1)

while len(threadlist) < max_threads and uid < 8000000:

uid+=1

thread2=threading.Thread(target=run,args=(uid,))

thread2.setDaemon(True)

thread2.start()

threadlist.append(thread2)

time.sleep(5)

运行很顺利:

INSERT

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值