简体   繁体   中英

php/mysql can't run same file twice at the same time

I'm trying to make a "robot" do some data processing but can't figure out how to run the same file twice and speed up the processing with multiple processes of same file.

My code:

while($stop != 1){

    $result = $mysqli->query("SELECT * FROM `data` WHERE `state` = 'nothing' ORDER BY `id` DESC");
    $row = $result->fetch_assoc();

    if(empty($row)){
        echo $i." stop";
        $stop = 1;
    }else{
        echo "do ".$i."<br/>";
        $mysqli->query("UPDATE `data` SET `state` = 'done' WHERE `id` = '{$row['id']}'");
    }

    $i++;
}

Pretty simple, right?

The file continues until there is no more state = 'nothing' in the database and it works fine with one file...

It just goes like:

do 1
do 2
do 3
do 4
do 5
do 6
do 7

(There are like 3000 rows in the database)

But if I run the same file second time it doesn't do anything and just waits for the other file to finish and then just writes "1 stop" meaning it did nothing even if I started the files at the same time.

I noticed if I start the both files at the same and cancel the execution of the first file - the second file just starts from 1 id even through like 500 of lines are already state = 'nothing'.

It got me thing now... is this a php or mysql problem? How can I make one file runned twice process different data and not hang up like this?

CentOS release 6.6 (Final)

PHP 5.3.28 (cli) (built: Jul 21 2014 12:19:25)

+-------------------------+------------------------------+
| Variable_name           | Value                        |
+-------------------------+------------------------------+
| protocol_version        | 10                           |
| slave_type_conversions  |                              |
| version                 | 5.5.38                       |
| version_comment         | MySQL Community Server (GPL) |
| version_compile_machine | x86_64                       |
| version_compile_os      | Linux                        |
+-------------------------+------------------------------+

try to use different query ex:

localhost/a.php?process=1

localhost/a.php?process=2

this will make your file run twice in the same time

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM