表 task 里有字段 id,filename,status.
当 status=0 时,需要对 filename 对应的文件进行处理,处理一个文件大约费时1秒.处理成功将 status 设为 1,错误设为 -1.
文件的处理顺序不限.select * from task where status=0 limit 60;一次读取 60 条记录进行处理.可是如果多开几个进程,问题就来了,其他进程已经读取但还没处理完的记录,会被重复读取并重复处理.各位大大有什么好方法没?
当 status=0 时,需要对 filename 对应的文件进行处理,处理一个文件大约费时1秒.处理成功将 status 设为 1,错误设为 -1.
文件的处理顺序不限.select * from task where status=0 limit 60;一次读取 60 条记录进行处理.可是如果多开几个进程,问题就来了,其他进程已经读取但还没处理完的记录,会被重复读取并重复处理.各位大大有什么好方法没?
UPDATE child_codes SET counter_field = counter_field + 1;
select * from task where status=0 limit 60 for update;
不过要注意表引擎必须是INNODB。因为MYISAM是表级锁定。
http://blog.chinaunix.net/u/29134/showart_478439.html
1. 如果当前进程没有 update,进程结束后会不会解锁对应的行?
2. 锁定的行其他进程读不了,所以跳到后面去读下面的 60 行?