表 task 里有字段 id,filename,status.
当 status=0 时,需要对 filename 对应的文件进行处理,处理一个文件大约费时1秒.处理成功将 status 设为 1,错误设为 -1.
文件的处理顺序不限.select * from task where status=0 limit 60;一次读取 60 条记录进行处理.可是如果多开几个进程,问题就来了,其他进程已经读取但还没处理完的记录,会被重复读取并重复处理.各位大大有什么好方法没?

解决方案 »

  1.   

    select .... for update
    == 思想重于技巧 ==
      

  2.   

    SELECT counter_field FROM child_codes FOR UPDATE;
    UPDATE child_codes SET counter_field = counter_field + 1;
    == 思想重于技巧 ==
      

  3.   


    select * from task where status=0 limit 60 for update;
    不过要注意表引擎必须是INNODB。因为MYISAM是表级锁定。
      

  4.   

    详细解释:
    http://blog.chinaunix.net/u/29134/showart_478439.html
      

  5.   

    select * from task where status=0 limit 60 for update;有两个问题:
    1. 如果当前进程没有 update,进程结束后会不会解锁对应的行?
    2. 锁定的行其他进程读不了,所以跳到后面去读下面的 60 行?
      

  6.   

    刚才用命令行试了下,不对啊...select * from task where status=0 limit 60 for update;运行后,下一个进程用同样的语句,一直在等前一个进程运行结束才有结果,前一个进程要运行一分多钟呢...我要的结果是:前一个进程锁定了前 60 行记录,那么下一个进程应该读取未锁定的下一个 60 行记录,并同时处理文件.