Php code optimization problem

a.php and b.php are available in the linux environment. Now the code relationship is as follows. It takes more than 6 seconds to execute a.php. How can you optimize it so that a.php can be executed within 3 seconds?
a.php

$test = new test();
$arr = ["","",""]
foreach($arr as $value){
    $test->check($value)
    }

b.php

class test
{
    public function check($value){
        sleep(2);
        //,,.
    }
}

the method I think of is to change the a.php to the following, but if the parameters of Chongqing and Shandong become an array and cannot be passed through cli (json_encode,serious and other methods have been tried, but b.php cannot be restored to an array after receiving it), what should I do?

$arr = ["","",""]
foreach($arr as $value){
    exec("php b.php $value > /dev/null &")
    }
Sep.16,2021

sleep (1);


the php single process is blocked, and you will definitely delay loading if you use sleep (2). Each cycle takes 2 seconds. So we need to find a way to make it multi-process execution.

The

php curl_multi_exec function can help you.


cli can of course be parameterized, but it can only be passed as a string. You can json_decode the array after json_encode,. But why not set up a queue. If your check needs to be executed for that long, there must be a lot of data.
set up a queue, throw the data in one by one, and then the processing process of b can set the number of open processes according to the size of the data. no, no, no. In this way, you can definitely finish processing in 1 second


you use cli, and you don't want a result. What you want is a queue. It just takes too long to query the database directly. You can also try NoSql


exec can not meet your needs. If there is / dev/null & , it is asynchronous, and a.php is finished at once. If not, it is still blocking. What's the difference between using exec or not?


Why sleep (2);?
A SQL operation usually does not manipulate 1s


but if the parameters of Chongqing and Shandong are changed into arrays, they cannot be passed through cli (json_encode,serious and other methods have been tried, but b.php cannot be restored to arrays after receiving them
).

I can revert to an array with json_encode

a.php

<?php
$arr = [[1,2,3],[4,5,6],[7,8,9]];
foreach ($arr as $val) {
    $val = json_encode($val);
    $b = __DIR__ . '/b.php';
    exec("nohup php $b $val >> b.out &");
}

b.php

<?php
$val = json_decode($argv[1]);
test::check($val);
class test {
    public function check($val) {
        sleep(2);
        print_r($val);
    }
}

b.out

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Array
(
    [0] => 4
    [1] => 5
    [2] => 6
)
Array
(
    [0] => 7
    [1] => 8
    [2] => 9
)

if you just write to the database, you can first splice it into SQL, and then execute the SQL query.


queue, or swoole

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b374a7-2c08b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b374a7-2c08b.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?