我正在使用python mysqldb测试mariadb更新,但它对我来说无法正常工作。当同时运行两个python脚本作为cronjob时,相同的行得到两次。当我在两个mysql控制台上测试时,一切都很顺利。使用innodb。我的代码:
cursor.execute("start transaction")
cursor.execute("SELECT id FROM t1 where tw1=2 AND tw2=1 AND tw3=3 order by id limit 1000 FOR UPDATE")
rows = [r for r in cursor.fetchall()]
ids = []
for row in rows:
ids.append(str(row['id']))
for i in range(2):
cursor.execute("SELECT id FROM t2 where tw1="sth" AND tw2 =0 AND tw3=0 LIMIT 1 FOR UPDATE")
d = cursor.fetchone()
if not d:
#here: insert to t2 and get last id as d_id
cursor.execute("update t2 SET tu1=1,tu2=500,tu3=0 WHERE id = %s" % d_id)
for row in rows:
#job with rows
cursor.execute( "Update t1 SET tw1=tw1+1 WHERE id IN (" + ','.join(ids) + ")")
cursor.execute("commit")
来自t1的tw1应该是最大值3,但它经常是4.当我把ids放到文件中时,我看到有2个不同的程序运行有相同的ID。我做错了什么?