PHP uses redis's blPop/brPop, and a server can only execute it once at the same time?
phpcn_u1582
phpcn_u1582 2017-05-16 12:59:45
0
2
785

The server uses the nginx php-fpm architecture, and redis uses connect to connect. Each network request should have a separate php-fpm process. I wrote a loop with blPop/brPop and sleep(5) in the loop. The browser opened two tabs and ran them, and then lpush loaded 4 data into list. I found that the tab that always ran first could read the first two data. In other words, blPop/brPop blocks all other blPop/brPops on the entire server that listen to the same key?
Here is the code I tested:

//堵塞出队列
public function test(){
    tool::load('hRedis.php');
    $timeOut = 20;//堵塞20秒

    $cn = 'test_blist';

    $ress = [];
    for($i=0;$i<2;$i++){
        $ress[] = hRedis::cacheListBPop($cn,$timeOut);//这里面封装了redis的blPop/brPop
        sleep(5);
    }

    print_r($ress);
}

Why is the process that runs later still blocked during the sleep period of the process that runs first?

We did further testing, removed the loop, executed each process only once, and used redis->close() after each read. We found that the end time of the two processes still differed by about 16 seconds.

For the last test, I put it on two servers again. This time it was not two processes on the same server, but two servers. I found that the end time of the two servers, the number of seconds different, was the number of seconds I inserted into the two servers. The time between pieces of data! No delays this time!

phpcn_u1582
phpcn_u1582

reply all(2)
某草草

First of all, Redis is single-threaded, and any requests sent to the server are queued and executed in order. So you said that each network request should have a separate process, which I think is incorrect.
blPop instruction blocks the client , not the server side, otherwise it will be fatal for single-threaded Redis.
After you open two tabs, it is equivalent to opening 2 clients. Before you open 2 tabs and there is no push data, these 2 The clients are both in blocking state, and your blocking time is set to 20 seconds.
When you push the data to the list, the two clients perform the brpop operation on the same key, then the client that executes the brpop command first can get the pop value, that is, the first tab page. sleep 5s, pop another data, and then output it. After the first client exits, if the second client is still executing the pop line, I think It should output subsequent values. If the opening interval between the two clients is very short, it will probably exit. You can adjust the opening time interval between the two clients and test it. Then tell me the answer. Thank you

Peter_Zhu

The sentence "Every network request should have a separate process" is wrong and has been changed to "Every network request should have a separate php-fpm process".
The second question is as you said, the second client will output subsequent values, but the time when the second client outputs the value is 16 seconds later on average than the first client. I want to know why. It's so late, can it be shortened? Why can't the second client listen at the same time as the first client?

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template