同时运行多个PHP脚本(数据库循环问题) [英] Running multiple PHP scripts at the same time (database loop issue)

查看:106
本文介绍了同时运行多个PHP脚本(数据库循环问题)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我同时运行10个PHP脚本,并在Linux上在后台处理.

I am running 10 PHP scripts at the same time and it processing at the background on Linux.

例如:

while ($i <=10) {
 exec("/usr/bin/php-cli run-process.php > /dev/null 2>&1 & echo $!");
 sleep(10);
 $i++;
}

run-process.php中,我在数据库循环方面遇到问题.其中一个进程可能已经将status字段更新为1,似乎其他php脚本进程没有看到它.例如:

In the run-process.php, I am having problem with database loop. One of the process might already updated the status field to 1, it seem other php script processes is not seeing it. For Example:

$SQL = "SELECT * FROM data WHERE status = 0";
$query = $db->prepare($SQL);
$query->execute();

while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
    $SQL2 = "SELECT status from data WHERE number = " . $row['number'];
    $qCheckAgain = $db->prepare($SQL2);
    $qCheckAgain->execute();
    $tempRow = $qCheckAgain->fetch(PDO::FETCH_ASSOC);

    //already updated from other processs?
    if ($tempRow['status'] == 1) {
        continue;
    }

    doCheck($row)
    sleep(2)
}

如何确保流程不会再次重做相同的数据?

How do I ensure processes is not re-doing same data again?

推荐答案

当您有多个进程时,您需要使每个进程都拥有某些记录集的所有权".通常,您可以通过使用limit子句进行更新,然后选择脚本仅拥有"的记录来进行此操作.

When you have multiple processes, you need to have each process take "ownership" of a certain set of records. Usually you do this by doing an update with a limit clause, then selecting the records that were just "owned" by the script.

例如,有一个字段指定记录是否可用于处理(即,值为0表示该记录可用).然后,您的更新会将字段的值设置为脚本进程ID,或该进程的某些其他唯一编号.然后,您在进程ID上进行选择.完成处理后,可以将其设置为完成"数字,例如1.更新,选择,更新,重复.

For example, have a field that specifies if the record is available for processing (i.e. a value of 0 means it is available). Then your update would set the value of the field to the scripts process ID, or some other unique number to the process. Then you select on the process ID. When your done processing, you can set it to a "finished" number, like 1. Update, Select, Update, repeat.

这篇关于同时运行多个PHP脚本(数据库循环问题)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆